Dynamic domain for M2O field without on change trigger in Odoo
I had a class in some module
class Projects(models.Model):
_name = 'crm.projects'
product_lines = fields.Many2many(comodel_name="p.lines")
And a class in another module
class CustomLead(models.Model):
_inherit = 'crm.lead'
project = fields.Many2one(comodel_name="crm.projects")
product_lines = fields.Many2one(comodel_name="p.lines")
Note that crm.projects.product_lines is M2M while crm.lead.product_lines is M2O.
The crm.projects
’s form has a button that opens wizard of the crm.lead
class and filters data in the crm.lead.product_lines
to be only the tags selected in the crm.projects.product_lines
Let’s explain that more…
You know that M2M field takes tags widget in the XML view while M2O takes the drop-down list view , So imagine p.lines
has A, B, C, D records. and in crm.projects
form I have selected only tags A, B in the product_lines
so after I click the button that opens crm.lead
wizard it will let me only select A or B in the crm.lead.product_lines
. That’s done easily using on change trigger dynamic domain on that field.
domain = {'product_lines': [('id', 'in', self.project.product_lines.ids)]}
The problem is that if I go to the screen of records created from that wizard and edit the field project_lines, the domain filter won’t work and you can see C, D also not only A,B.
Solution to this problem using the following modification in the class CustomLead
That’s it! now it works as expected.