Dynamic domain for M2O field without on change trigger in Odoo

Dynamic Gear
image: https://hipwallpaper.com/view/Ars7k2

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 ’s form has a button that opens wizard of the class and filters data in the to be only the tags selected in the

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 has A, B, C, D records. and in form I have selected only tags A, B in the so after I click the button that opens wizard it will let me only select A or B in the . 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

That’s it! now it works as expected.

Between Theory and Industry