Search code examples
xmlodoo-8

Make a fields Many2many depend on another fields Many2one in odoo


I am new in Odoo and i have two fields

gathering_id = fields.Many2one('health_administrator.gathering_model')
key_letter_id = fields.Many2many('health_administrator.key_letter')

The fields key_letter_id depend on fields gathering_id ex: when i make a selection on gathering_id, the fields is loaded depend on the selection in the fields gathering_id .

How i can do that

Thanks.


Solution

  • Onchange many2one filed in odoo

    Today you will learn how to create a dependent drop down (many2one) fields in Odoo. For example I have two many2one fields (campus_id and department_id), and we want to change the department on the basis of campus field.

    campus_id = fields.Many2one('model.campus', string="Campus Name") department_id = fields.Many2one('model.department', string="Department Name")

    1    @api.onchange('campus_id')
    2    def _campus_onchange(self):
    3        res = {}
    4        res['domain']={'department_id':[('campus_id', '=', self.campus_id.id)]}
    5        return res
    

    Code Description: In line no 1 we use @api.onchange decorator for campus_id. It means whenever campus changes or select a campus from many2one filed do the following line of code (3,4,5). In line no 3 we declare a dict named res. In line no 4 we use domain to change the department field.

    Source : Learn Programming Onchange many2one filed in odoo