来自一半Odoo 10的字段的功能

时间:2018-08-16 13:53:00

标签: python odoo

我注意到了什么,一旦我在Odoo中激活了布尔值,就会显示该消息。我希望仅在保存表单时显示“ half_pension on”消息。我怎样才能做到这一点?

保存按钮自动包含在Odoo中

随便–

我的课:

class ResPartner_school(models.Model):

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

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

我的功能:

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

感谢您的帮助!

编辑:

@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'是我当前的课程

1 个答案:

答案 0 :(得分:1)

无论最终将使用print语句如何,要使用保存按钮作为更改内容的调用,您都必须覆盖/扩展模型的create()write()方法。 / p>

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

onchange装饰器用于在客户端中更改字段时调用装饰的方法。然后,更改本身将通过“保存”按钮保存。因此,在这种情况下,onchange行为是错误的决定。