How to override a Related field

In Odoo you can define a related field by setting up the related attribute in the field definition, more info you can find for this type of fields in the post.

In case when you want to override a related field and to change the related attribute with computed you need to define a computed method and additionally to set the related attribute to related=”

For example, the field free_member is a related field in Odoo, and now you need to change the functionality in the way that you need to make this field computed. You can do that in the following way:

from openerp import models, fields, api

class PartnerContact(models.Model):
    _name = 'partner.contact'

    free_member = fields.Boolean(related='', computed="_get_free_member", store=True)
 
    @api.one()
    def _get_free_member(self):
        if self.user:
            self.free_member = self.user_id.free_member

If you think that this is completely same as using a related field, check our next post and see why it is not.

Share the knowledge!

Leave a Reply

Your email address will not be published.