Search Here

Set Domain Filter for Many2one Field in Odoo13

 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.


Post a Comment

10 Comments

  1. Do I need to use a function domain in a case in a situation, where I have a field in my object let's say it's country_id = fields.Many2one('res.country') and I have One2many fields of 'hr.employee' and I want to limit that so only records where Employee's country is the same as my country_id So I thought domain would be something like [('country_id', '=', 'employee_id.country_id')] but that crashed on frontend error that attribute does not exist or something like that...

    ReplyDelete
  2. Do I need to use a function domain in a case in a situation, where I have a field in my object let's say it's country_id = fields.Many2one('res.country') and I have One2many fields of 'hr.employee' and I want to limit that so only records where Employee's country is the same as my country_id So I thought domain would be something like [('country_id', '=', 'employee_id.country_id')] but that crashed on frontend error that attribute does not exist or something like that...

    ReplyDelete
    Replies
    1. could you please share your code here.

      Delete
    2. Well it should be something like this:

      class Your_Model(models.Model):
      _name='your.model'

      country_id = fields.Many2one('res.country')
      employee_id = fields.Many2one('hr.employee','Employee',required=True,domain="[('country_id', '=', 'employee_id.country_id')]")

      Delete
    3. if you want to put a domain like above, than i would suggest you to use functional domain instead of inline...

      Delete
  3. hi, will you please help to the problem below:
    I want to add domain filter on field ( field2, field3 ), the filter is based on another Many2one field ( field3 is based on -> field2 is based on -> field1 )

    for example:

    1) user select a value in field1 (will show all countries to select one)

    2) field2 should be filtered as per field1 selected value (filter to select states in selected country)

    3) field3 should be filtered as per field2 selected value (filter to select cities in selected state)

    please help.
    regards

    ReplyDelete
  4. I would like to filter m2o field, but not by default name (_rec_name).

    class LecturerWorkday(models.Model):
    _name = 'lecturer.workday'
    _rec_name = 'lecturer_id'

    name = fields.Selection([('sunday','Sunday'),('monday','Monday'),('tuesday','Tuesday'),
    ('wednesday','Wednesday'),('thursday','Thursday'),('friday','Friday'),('saturday','Saturday'),
    ], default='sunday',string="Workday", required=True)
    lecturer_id = fields.Many2one('school.lecturer', string="Lecturer Name", invisible=True)

    class SchoolLecturer(models.Model):
    _name = 'school.lecturer'

    name = fields.Char(string="Lecturer Name", required=True)
    workday_id = fields.Many2one("lecturer.workday", string="Workday ID")

    class LecturerTimeoff(models.Model):
    _name = "lecturer.timeoff"

    lecturer = fields.Many2one('school.lecturer', string="Lecturer Name")
    day_m2o = fields.Many2one('lecturer.workday', string="Lecturer Workdays")
    reason = fields.Char("Time off Reason")

    @api.onchange('lecturer')
    def get_lecturer_workday(self):
    day_obj = self.env['lecturer.workday'].search([('lecturer_id', '=', self.lecturer.id)]).mapped('name')
    for rec in day_obj:
    day_list.append(rec)
    res = {}
    res['domain'] = {'day_m2o': [('name', '=', day_list)]}
    return res
    print (res)
    My question is:

    When I choose lecturer name, day_m2o should display the workday of selected lecturer name. I have been trying to compute it as above, but the result is still display lecturer name, instead of workday. How to fix this?
    Thanks for your help

    odoo
    Share

    ReplyDelete
  5. Hi..how to set m2o field not using name or _rec_name?
    I have 2 fields:
    field1 : display m2o field (name)
    field2 : display m2o field (email)
    thanks

    ReplyDelete