In this article, you will learn how to create a Many2one field in Odoo13 and set the filter domain on Many2one field using different ways.

Set Domain Filter for Many2one Field in Odoo13


How to Create Many2one Field in Odoo13

If you want to select the value from the dropdown list, then in Odoo13 you can achieve this using the Many2one field. You can select only one value from the dropdown field. If you want to select multiple values from the dropdown that you have to use the Many2many field.

employee_id = fields.Many2one('hr.employee','Employee',required=True)

In the above code, 'hr.employee' is a model name, which you want to show records in the dropdown.

Set Domain Filter on Many2one Field in Odoo13

You can filter the records in the Many2one field using two ways. The first one is called the inline domain (which you provide on Many2one) field. And the other way is by creating a method and call that method in Many2one field, that will return filtered records to the Many2one field.

employee_id = fields.Many2one('hr.employee','Employee',required=True, domain="[('state','=','Current')]")

The domain in the above code will filter and load the records in the Many2one field which have in "Current" state. The above method is useful when we have this (state) field in the same model like 'hr.employee' and we want to put some simple domain like state. What If? we want to load records in Many2one based on custom logic like based on user group or login user group. To achieve this we will use the function domain.