PseudoWithK
PseudoWithK

Reputation: 321

Create function from a Boolean field odoo 10

I would like to create a function that checks whether the value of a boolean is True or False. If this boolean is True, I would like to execute a sequence of instructions. This function is created in the same class as my booleen field. Do I have to use a function decorator?

My class :

class ResPartner_school(models.Model):

    _name = 'ecole.partner.school'
    _order = 'id desc'

    half_pension = fields.Boolean(string='Restauration Scolaire', copy=False)

My Function :

def synchroHalfPension(self):
    if self.half_pension:
        print "BOOLEAN TRUE"
    else:
        print "BOOLEAN FALSE"

How to simply verify that my function works ?

I am a beginner.

Thank you

EDIT : The first part of my problem is solved and thank you. Now I notice something, the message is displayed as soon as I activate the boolean in Odoo. I would like the "half_pension is on" message to appear only when I save my form. How can I do that? cordially –

Upvotes: 1

Views: 1063

Answers (1)

Atul Arvind
Atul Arvind

Reputation: 16763

It is really simple, you can decorate your method with api.onchange,

@api.onchange('half_pension')
def synchroHalfPension(self):
    if self.half_pension:
        print "half_pension is on"
    else:
        print "half_pension is off"

above function will be called whenever the boolean will change.

hope this helps!

Upvotes: 1

Related Questions