Wednesday 20 January 2016

Create many2one and one2many relationship in OpenERP


openerp.jpeg
In OpenERP we can create many2one and one2many relationships between models very easily by creating many2one and one2many fields. You just need to declare a field in _columns and then using this field normally in the rest programming and views as we normally do.
I created a small module to explain called hospital, I have two objects patient and doctor. To establish relationship between these two entities or objects, create doctor_id field in patient model as many2one, and patient_id as one2many in doctor model.
The relationship can be explained as one doctor with many patients and many patients going to a single doctor.
Now create a many2one field as:
in class patient(osv.osv):
  1. 'doctor_id':fields.many2one('doctor','Doctor'),
and in class doctor(osv.osv):
  1. 'patient_id':fields.one2many('patient','doctor_id',),
Basic syntax for declaring a many2one field:
  1. fields.many2one(
  2.       'object.name',
  3.       'field name',
  4.       optional parameters)
where first parameter is the model to which the relation is to be created, and field name is the name for the field.
declaring a one2many field:

To view the full blog about Create many2one and one2many relationship in OpenERP please visit Findnerd.

No comments:

Post a Comment