pythonselectionodoo-12

How do I get selection field value in odoo?


I have a selection field on a model called hr.overtime.rule like this:

buttons = fields.Selection([('per_day', 'Per Day'), ('monthly', 'Monthly')])

Now I have another model which, based on selected value it should do some computations. The logic is that when it is per_day it should do different computations and when in monthly it should do again different computations and that's why I need to check with an if statement whether is per_day or monthly.

What have I tried:

Thanks for your time fellas! I'd really appreciate your thoughts into this.


Solution

  • What you want to do is performing computations based on the value of a specific field, in this case, what I can suggest is to use an onchange function on that field:

    example:

    #better rename to hr_overtime_rule_id
    button_id = fields.Many2one('hr.overtime.rule')
    
    @api.onchange('button_id')
    def onchange_button_id(self):
        if self.button_id.buttons == 'per_day':
            computations1()
        if self.button_id.buttons == 'monthly':
            computations2()
    

    make sure your field grabs the object, otherwise you may need to define a new One2many field in the original model to link it with the current model using the reverse keyword