What is related field and how to define it

In such case when you need to show the value of a field from a relational model to the current model you can do that by defining a new related field. To do that you need in the field definition to define attribute related with value sequence of field names. By default, the value of the related field is not stored in the database. If you want to store it you need to define attribute store with value True.

Below you can find an example of using a related field in the ContactPartner model in order to show if it is a free member on its views.

from openerp import models, fields

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

    user_id = fields.Many2one('res.users', 'User')
    free_member = fields.Boolean(related='user_id.free_member')


  1. Najlae

    hi , have a problem with my related field , i have two classes : nomenclature and projet_ligne i want to get the value of ‘ sous’ on ‘eta’ so there s my code

    class nomenclature(models.Model):
    _name = ‘nomenclature’

    name = fields.Char(‘Nom de la nomenclature’,required=True)
    quantite = fields.Integer(‘Quantité’,required=True)
    sous= fields.Boolean(‘sous’)

    class projet_ligne(models.Model):
    _name = ‘projet.ligne’
    #name = fields.Char(‘nom du sous essaie’,required=True)
    projet = fields.Many2one(‘projet’,required=True)
    date= fields.Date()
    reference= fields.Char(‘Réference’)
    nature= fields.Char(‘Nature’)
    dateprelevement= fields.Date()
    lieuprelevement= fields.Char(‘lieu’)
    etatvalider= fields.Boolean(‘Validé’)
    eta= fields.Boolean(related=’nomenclature.sous’)

    It doesn t work :/

  2. Betone

    You need to put apostrophe to user_id.free_member

    free_member = fields.Boolean(related=’user_id.free_member’)

    so you don’t get an error


    1. admin

      For a many2one field, you would need to define the relation as usually and additionally need to provide “related” attribute.

Leave a Reply

Your email address will not be published. Required fields are marked *