In this article you will learn how to put domain on one2many field on the basis of parent model field. To achieve this I am going to use of context and many2many computed field.
Problem Statement
I have a parent model "parent.model" which have some fields, and a child model "child.model" (one2many) in a parent model. Now I want to add or put a domain filter on one2many field on the basis of some field which parent model have. For example I have semester_id field in one2many field of ("child.model") and I want to put domain filter on that field on the basis of my parent model ("parent.model"). To achieve this functionality we need some modification in our code.
- Pass parent field value (active_id) as a context in our one2many field
- Create a many2many computed field and fill that field value by using context, which we have get in point no 1.
- Use that many2many field as a domain in xml
Model.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class parent_model(models.Model): | |
_name = 'parent.model' | |
field_1_id = fields.Many2one('model.c') | |
field_2_ids = fields.One2many('child.model', 'parent_id') | |
class child_model(models.Model): | |
_name = 'child.model' | |
parent_id = fields.Many2one('parent.model') # We will set the default value of that field by using context | |
semester_id = fields.Many2one('model.semester') # I want to put/add domain filter on that field (on the basis of parent model field) | |
compute_semester = fields.Many2many('model.semester', compute="_compute_semester") # We will use this computed field to filter our one2many field (semester_id) | |
@api.multi | |
@api.depends('parent_id') | |
def _compute_semester(self): | |
if self.env.context.get('default_parent_id'): | |
parent_obj = self.env['parent.modelt'].search([('id', '=', self.env.context.get('default_parent_id'))]) | |
if parent_obj: | |
semester_obj = self.env['model.semester'].sudo().search([('department_id', '=', parent_obj.department_id.id)]) | |
self.compute_semester = semester_obj.ids |
XML
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<record id="parent_model_id" model="ir.ui.view"> | |
<field name="name">parent.model.form</field> | |
<field name="model">parnet.model</field> | |
<field name="arch" type="xml"> | |
<form string="Parent Model"> | |
<sheet> | |
<field name="field_1_id"/> | |
<field name="field_2_ids" context="{'default_parent_id':active_id}"> # Pass parent model id (active_id) as a context in our one2many field | |
<tree editable="bottom" > | |
<field name="semester_id" domain="[('id','in',compute_semester[0][2])]"/> | |
</tree> | |
</field> | |
</sheet> | |
</form> | |
</field> | |
</record> |
0 Comments