Skip to Content
Menu
This question has been flagged

When using the @api.onchange decorator, I was able to set a domain on a Many2one field based on the value of another field when it is changed. However, when I open an existing record and select the Many2one field, the domain does not get applied and all records are displayed. This means that the domain set using @api.onchange is not being applied when the record is opened and the field is selected

​class AccountMoveInherited(models.Model):
​ _inherit = 'account.move'
     ​project_name = fields.Many2one('res.partner.project',string="Project")
          ​ po_number = fields.Many2one('res.partner.po',string="PO")
          ​ customer_reference = fields.Char(string="Customer Reference")
           ​contact_person = fields.Many2one('res.partner',string="Contact Person")


​@api.onchange("partner_id")
​​def set_domain_for_project_name(self):
​​if self.partner_id:
​  domain = [('contact_id', '=', self.partner_id.id),('closed_boolean', '=', False)]
                    ​  result = { 'domain': {'project_name': domain}}
                    ​  return result

Avatar
Discard
Best Answer

Hi,

If You want to set a domain for a many2one you don't need to add on change, you can directly add a domain for your field:

Eg:- 

​project_name = fields.Many2one('res.partner.project',string="Project",domain = "[('contact_id', '=', self.partner_id.id),('closed_boolean', '=', False)]") 

Also, refer to these forums: 

https://www.odoo.com/forum/help-1/how-to-add-a-domain-to-a-many2one-field-in-odoo-179690

Regards

Avatar
Discard
Related Posts Replies Views Activity
2
Mar 23
1696
2
Dec 24
2792
2
Jun 24
9339
0
May 23
1057
1
Sep 22
2457