PseudoWithK
PseudoWithK

Reputation: 321

Odoo V10 - Calculated field

Here is my calculated field:

half_pension_days_value = fields.Integer(string='Day value', copy=False, compute='convert_bin_dec')

Here is my function:

@api.multi
def convert_bin_dec(self):
    if self.half_pension:
        print "Je suis dans la fonction convert"
        self.half_pension_days_value = 5

Encountered problem :

As soon as I launch this function thanks to the state of my boolean half_pension, my print starts looping. I guess the problem is at this line -> self.half_pension_days_value = 5.

But why ?

EDIT : In order

@api.model
def create(self, vals):
    record = super(ResPartnerSchool, self).create(vals)
    record.convert_bin_dec()
    return record


@api.multi
def write(self, vals):
    result = super(ResPartnerSchool, self).write(vals)
    self.convert_bin_dec()
    return result

Here my error -> RuntimeError: maximum recursion depth exceeded while calling a Python object Thank you

Upvotes: 1

Views: 320

Answers (2)

Quentin THEURET
Quentin THEURET

Reputation: 1232

Your problem is the following : the convert_bin_dec() method calls the write() method of your object when you do this : self.half_pension_days_value = 5 and the write() method calls again the convert_bin_dec() method, so you have recursive calls.

To fix this kind of issue, you have to use the @api.depends decorator.

Try with this :

@api.multi
@api.depends('half_pension')
def convert_bin_dec(self):
    for record in self:
        if record.half_pension:
            print "Je suis dans la fonction convert"
            record.half_pension_days_value = 5

Do not forget to remove the self.convert_bin_dec() in the write()and create() methods.

Upvotes: 1

Pawan Kumar Sharma
Pawan Kumar Sharma

Reputation: 1168

Hennion,

You said that half_pension is your boolean field. Then try this code:

@api.multi
def convert_bin_dec(self):
    for rec in self:
        if rec.half_pension == True:
            print "Je suis dans la fonction convert"
            rec.half_pension_days_value = 5

or You can break your function.it limit multiple entries.

@api.multi
def convert_bin_dec(self):
    for rec in self:
        if rec.half_pension:
            print "Je suis dans la fonction convert"
            rec.half_pension_days_value = 5
            break

Upvotes: 1

Related Questions