Reputation: 463
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
.
Imported the class to my current working python code: from .hr_attendance_policy import hr_overtime_rule
then I checked the value like this: value = hr_overtime_rule.buttons
. It works to an extended way, it is just stuck at only one value: monthly
Made a Many2one
relation field: button_id = fields.Many2one('hr.overtime.rule')
it works if you do if not
or if
but I specifically want either per_day
or monthly
Thanks for your time fellas! I'd really appreciate your thoughts into this.
Upvotes: 0
Views: 2288
Reputation: 144
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
Upvotes: 1
Reputation: 1
In order to get the value of selected field you can also do this:
var = fields.Many2one('your.model')
and then var2 = fields.Selection(related='var.technical_name')
and after doing so, you can check it with if statements
Upvotes: 0