PseudoWithK
PseudoWithK

Reputation: 321

Function from a field that works half Odoo 10

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?

The save button is automatically included in Odoo

cordially –

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 :

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

Thanks you for help !

EDIT :

@api.multi
def changed_half_pension(self):
    for record in self:
        if record.half_pension:
            print "half_pension is on"
        else:
            print "half_pension is off"

@api.model
def create(self, vals):
    record = super('ecole.partner.school', self).create(vals)
    record.changed_half_pension()
    return record

@api.multi
def write(self, vals):
    result = super('ecole.partner.school', self).write(vals)
    self.changed_half_pension()
    return result

'ecole.partner.school' is my current class

Upvotes: 1

Views: 37

Answers (1)

CZoellner
CZoellner

Reputation: 14768

Whatever the print statements will be in the end, to use the save button as call for changing something you have to override/extend create() and write() methods of your model.

class ResPartner_school(models.Model):

    @api.multi
    def changed_half_pension(self)
        for record in self:
            if record.half_pension:
                print "half_pension is on"
            else:
                print "half_pension is off"

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

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

The onchange decorator is used to call the decorated method whenever the field is changed in the client. The change itself will be then saved by the button "save". So onchange behaviour is the wrong decision in this case.

Upvotes: 1

Related Questions