pythonodooodoo-8table-relationshipsone2many

Odoo multiple many2many or One2many fields on single model


I'm inheriting the project.project module where I've added multiple Many2many fields: 'Buyers', 'Sellers', 'Contacts', etc. All related to res.partner. Know when I add multiple records to one of the fields, for example add some buyers. These buyers are automatically filled in in the sellers, contact fields too.

Any idea how I can prevent this, I understand why this is happening since the many2many creates a table with project_id and res_partner_id and doesn't know wheither it was created from the sellers, buyers or contacts field.

Thanks in advance.


Solution

  • Have you specified a table name for your Many2Many field? with the attribute "relation" because if you passed all the time the same two related field name. The table while be creating all the time with the same name by default.

    Explication:

    In your case you have

    sellers_ids = fields.Many2many(
        comodel_name='res.partner',column1='partner_id', column2='project_id', 
    ) 
    
    contact_ids = fields.Many2many(
        comodel_name='res.partner',column1='partner_id', column2='project_id', 
    ) 
    

    Odoo will be creating two table with the same name. "partner_id_project_id_rel". At the moment where you create a record in one Many2many the same table will be updating.

    But if you specify a relation name. Two tables with a different name will be creating. Like this.

     sellers_ids = fields.Many2many(relation='sellers_project_rel',
        comodel_name='res.partner',column1='partner_id', column2='project_id', 
    ) 
    
    contact_ids = fields.Many2many(relation='contact_project_rel',
        comodel_name='res.partner',column1='partner_id', column2='project_id', 
    )