Skip to Content
Menu
This question has been flagged
1 Reply
5333 Views

Hello,

I've made 2 models :

class ModelA(models.Model):

    _name = "model.a"

    _description = "Will be based on the planning module to handle work "

    _rec_name = "based_on_role"

    

    work_day = fields.Date('Working day', default=fields.Date.today)

    note = fields.Char('Note')    

    state = fields.Selection([

            ('new', 'Information'),

            ('confirmed', 'CONFIRMED'),

            ('approved', 'APPROVED'),

            ('cancel', 'CANCEL'),

            ],default='new')

    o_2_m = fields.One2many('model.b', 'moda_modb', string="ABC XYZ")            

    based_on_role = fields.Many2one('planning.role', string="Open Shift")


In model_b.py, I inherited the planning splot model of the planning module which already has data in the field: employee_id, role_id,... 

class ModelB(models.Model):

    _name = "model.b"

    _inherit = "planning.slot"

    _description = "Inherits the planning.slot model of the planning module"

    moda_modb = fields.Many2one('model.a')


I want to show/load already data in the field: 'employee_id', 'role_id' of the model planning slot in o_2_m field of the model_a's form view, based on the change of based_on_role field.

For Ex: 

If the user selected the data: 'Chef', in field: based_on_role

Then in o_2_m field will only show/load all user name which has the role: 'Chef'

and so on...


I'd tried to create the onchange function for the field like that:

@api.onchange('based_on_role')

    def onchange_based_on_role(self):

        for rec in self:

            lines = [(5, 0, 0)]               

            if rec.based_on_role:

                for line in self:

                    data_planning_slot = self.env["planning.slot"].search([(

                                                                            'employee_id', '=', line.employee_id.id,

                                                                           'role_id', '=', line.role_id)])               

                val = {

                    'employee_id' : line.data_planning_slot.employee_id.id,

                    'role_id' : line.data_planning_slot.role_id,

                }

                lines.append((0, 0, val))

            rec.o2m = lines

But it's wrong!


Please help! 

Thank you!                            

Avatar
Discard
Best Answer

Good morning, 

 

How To Load Default Values For One2many Fields in Odoo


I hope this link will help u 

Avatar
Discard
Related Posts Replies Views Activity
0
Sep 20
1739
0
Apr 16
2202
3
Mar 16
12670
1
Mar 15
4544
1
Mar 15
11348