Meet Us at GITEX Stand #SRE-22, Sheikh Rashid Hall, India Pavilion

Schedule Meeting
      +91-79-2975-0867,+91-903-347-2982,+91-987-935-4457   contact@serpentcs.com

Relational Fields in OpenERP

  OpenERP (OpenObject Framework) gives you the ability to link or relate records in a "Parent/Child" hierarchy. This is done with link to parent/child record fields which we also call relational fields. Mainly there are 3 relational fields:

  • many2one (Relationship towards a parent object (using a foreign key))
  • one2many (Virtual relationship towards multiple objects (inverse of many2one))
  • many2many (Bidirectional multiple relationship between objects)
  • related (Shortcut field equivalent to browsing chained fields)

many2one:

This type of field associates this object to a parent object via this Field. For example "Product Category" "Product" belongs to would Many to one. i.e Many Products will belong to a Product Category. Syntax: fields.many2one('object.name', 'Field Name', optional parameters) Where:
  • object.name: _name of destination object (required),
  • Field Name: String/Label of the field,
  • Optional parameters:

ondelete: What should happen when the resource this field points to is deleted. Predefined values are "cascade", "set null", "restrict", "no action", "set default". Default value is "set null".

For more information about ondelete action refer: Referential Actions

required: True (Makes field mendatory) readonly: True (Won't allow to edit) select: True - (Creates an index on the Foreign Key field)

  • Example: 'category_id': fields.many2one('product.category', 'Category'),
Many2one
Delete
You can find source code (class) for many2one field in:
  • v6.0: server/bin/osv/fields.py: class many2one(_column)
  • v6.1 & 7.0: server/openerp/osv/fields.py: class many2one(_column)
 

one2many:

This field provides a virtual relationship towards multiple objects (inverse of many2one). Syntax: fields.one2many('object.name', 'field_id(m2o)', 'Field name', optional parameter) Where:
  • object.name: _name of destination object (required)
  • field_id: field name of inverse many2one, i.e. corresponding foreign key (required)
  • Optional parameters:

invisible: True/False states: ? readonly: True/False

  • Example: I want to display that how many products are there which belong to a particular category. Then I will use one2many field in Product Category. 'product_ids': fields.one2many('product.product', 'category_id', 'Products'),
one2many

Delete
It is mandatory to define many2one field in destination object (in our case product.product). You can find source code (class) for one2many field in:
  • v6.0: server/bin/osv/fields.py: class one2many(_column)
  • v6.1 & 7.0: server/openerp/osv/fields.py: class one2many(_column)
 

many2many:

Encapsulates the logic of a many-to-many bidirectional relationship, handling the low-level details of the intermediary relationship table transparently. A many-to-many relationship is always symmetrical, and can be declared and accessed from either endpoint model. Syntax: fields.many2many('object.name', 'relation object (table name)', 'current.object.id', 'other.object.id', 'Field Name') Where:
  • object.name: _name of destination object (required)
  • relation object: relationship table to use (required in v6). In v6.1 & 7.0 it has been become an optional name of the intermediary relationship table. If not specified, a canonical name will be derived based on the alphabetically-ordered model names of the source and destination (in the form: ``amodel_bmodel_rel``). Automatic naming is not possible when the source and destination are the same, for obvious ambiguity reasons.
  • current.object.id: name of field in rel table storing the id of the current object (required in v6). In v6.1 & 7.0 it has been become an optional name for the column holding the foreign key to the current model in the relationship table. If not specified, a canonical name will be derived based on the model name (in the form: `src_model_id`).
  • other.object.id: name of field in rel table storing the id of the target object (required in v6). In v6.1 & 7.0 it has been become an optional name for the column holding the foreign key to the destination model in the relationship table. If not specified, a canonical name will be derived based on the model name (in the form: `dest_model_id`)
  • Optional parameters:

invisible: True/False states: ? readonly: True/False

  • Example: 'category_id': fields.many2many('res.partner.category', 'partner_categ_rel', 'partner_id', 'category_id', string='Tags')
Many2many
Delete You can find source code (class) for many2many field in:
  • v6.0: server/bin/osv/fields.py: class many2many(_column)
  • v6.1 & 7.0: server/openerp/osv/fields.py: class many2many(_column)

related:

Field that points to some data inside another field of the current record. Sometimes you need to refer to the relation of a relation. For example, suppose you have objects: City -> State -> Country, and you need to refer to the Country from a City, you can define a field as below in the City object:
_columns = {
'state_id': fields.many2one('res.country.state', 'State'),
'country_id': fields.related('state_id', 'country_id', type="many2one", relation="res.country", string="Country"),
}
Where:
  • The first set of parameters are the chain of reference fields to follow, with the desired field at the end.
  • type is the type of that desired field.
  • Use relation if the desired field is still some kind of reference. relation is the table to look up that reference in.
You can find source code (class) for many2many field in:
  • v6.0: server/bin/osv/fields.py: class related(function)
  • v6.1 & 7.0: server/openerp/osv/fields.py: class related(function)