Jugert Mucoimaj
Jugert Mucoimaj

Reputation: 463

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.

Upvotes: 0

Views: 2288

Answers (2)

Josephus
Josephus

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

Ricky Low
Ricky Low

Reputation: 1

In order to get the value of selected field you can also do this:

  1. Find a relation of your current working model and the other model.
  2. After finding the relation(or just create one) you can access it with a related field
  3. The syntax should be smth like 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

Related Questions