How To Update One2many Field From OnChange of Field in Odoo 17
CelineGeorge1
1,089 views
11 slides
Jul 09, 2024
Slide 1 of 11
1
2
3
4
5
6
7
8
9
10
11
About This Presentation
There can be chances when we need to update a One2many field when we change the value of any other fields in the form view of a record. In Odoo, we can do this. Let’s go with an example.
Size: 789.78 KB
Language: en
Added: Jul 09, 2024
Slides: 11 pages
Slide Content
How To Update One2many Field From OnChange of Field in Odoo 17 Enterprise
Introduction Enterpr ise There can be chances when we need to update a One2many field when we change the value of any other fields in the form view of a record. In Odoo, we can do this. Let’s go with an example.
Enterprise To demonstrate the process, let’s create a module named ‘college_management’ which has the structure like this.
Enterprise Here, the module is for the admission purpose. For that, we have a model ‘college.class’ which stores the details of each class. For every class, the course, semester and multiple subjects can be selected as this.
Enterprise The python code for the college.class model is as class CollegeClass(models.Model): _name = 'college.class' _description = 'College Class' semester = fields.Selection( [('one','1'),('two','2'),('three','3'),('four','4'),('five','5'),('six','6')], string='Semester No') course = fields.Selection([('cs','Computer Science'),('math','Mathematics'), ('phy','Physics')]) subject_ids = fields.One2many('class.subject', 'class_id')
Enterprise And t he python code for the model class.subject which is used as the comodel for the One2many field ‘subject_ids’ is as class ClassSubject(models.Model): _name = 'class.subject' _description = 'Class Subject' name = fields.Char('Subject Name') student_id = fields.Many2one('student.details') class_id = fields.Many2one('college.class') It stores the name of the subject and also, there are two Many2one fields which is the inverse field used for creating one2many fields in ‘student.details’ and ‘college.class’ models.
Enterprise Now, for the Student details page, many fields are created to store the basic information of the students. Along with that, the Many2one field ‘Class’ is there from which we can select the previously created class for the student like this.
Enterprise The field ‘Subjects’ shown in the form page of Student Card is of type One2many which is defined as subject_ids = fields.One2many('class.subject', 'student_id', string="Subjects")
Enterprise Our aim is on changing the Many2one field ‘Class’, update this ‘subject_ids’ field in student.details with the same values we saved for the selected class. For that, add an onchange method as @api.onchange('class_id') def onchange_subject_ids(self): self.subject_ids = self.class_id.subject_ids On every change for the field class_id (Class), the record’s subjects will get updated. ‘self.subject_ids’ is the record’s Subjects and ‘self.class_id.subject_ids’ is the record’s selected class’ subjects.
Enterprise That is, let’s change the class here as ‘Computer Science - Semester: 2’. Then it fills that class’s subjects below.
For More Info. Check our company website for related blogs and Odoo book. Check our YouTube channel for functional and technical videos in Odoo. Enterprise www.cybrosys.com