Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Create many2one and one2many relationship in OpenERP

    • 0
    • 1
    • 0
    • 1
    • 6
    • 0
    • 0
    • 0
    • 11.4k
    Comment on it

    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):

    'doctor_id':fields.many2one('doctor','Doctor'),
    

    and in class doctor(osv.osv):

    'patient_id':fields.one2many('patient','doctor_id',),
    

    Basic syntax for declaring a many2one field:

    fields.many2one(
            'object.name',
            'field name',
            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:

    fields.one2many(
            'other.object.name',
            'Field relation id',
            'Fieldname',
            optional parameter)
    

    Optional parameters:

    1. required = true/false: means a mandatory field,
    2. readonly = true/flase: means not editable field,
    3. invisible = true/false: to be visible in the view or not, 4 states: these can be defined as you wish,.

 6 Comment(s)

  • Hello, I would like to have a possibility to link sale.order.line to Purchase order, in the way that is it linked to purchase.order.line, is it technically possible? Here is my code, but it doesn't work: class purchase_order(models.Model): _inherit = "purchase.order" order_line = fields.One2many('sale.order.line', 'purchase_id_in_sol', 'Sale Order Lines', states={'approved':[('readonly',True)], 'done':[('readonly',True)]}, copy=True)

    class sale_order_line(models.Model): _inherit = 'sale.order.line' purchase_id_in_sol = fields.Many2one('purchase.order', string='Sous-traitant order Reference')

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: