Search code examples
odooodoo-12

Odoo 12 : how to add domain depending on related field?


I have two many2many related field, let's call them field_one and field_two and one many2many, let's call it field_three.

Here are the definition of the three fields :

field_one = fields.Many2many(comodel_name="res.partner", related="project_id.field_one")
field_two = fields.Many2many(comodel_name="res.partner", related="project_id.field_two")
field_three = fields.Many2many(comodel_name="res.partner", relation="acco", column1="ac", column2="co")

When the user want to add partner(s) (res.partner) in the field_three, those partner(s) that already in field_one and field_two should not be in scrooling list in the field_three.

How to i proceed to do that? Thank you very much.<

I used api.onchange("field_one", "field_two") but it doesn't work.

PS : We are in "project.task" model here.


Solution

  • Use XML domain:

    For old version of Odoo (I think < 11) there was a solution for this :

      <field name="field_three"
        ....
        ....
        domain = [('id', 'not in' , field_one[0][2])]/>
    

    This solution doesn't work but after some debuging I found out that Odoo store the selected Ids in the _store attribute. If you want to exclude selected partner from the search list use this domain.

           domain = [('id', 'not in' , field_one._store), ('id', 'not in', field_two._store)]
    

    This worked for me just fine in Odoo 11 hope it work for you.