Odoo 10-重载Python方法

时间:2019-05-21 07:39:53

标签: python odoo

我有一个问题。我有一个函数,称为重载write()方法。 我有另一个功能与之无关,但它修改了字段。

除了必须调用调用第一个函数的write方法...

当某些字段受到影响时,有没有一种方法不被调用?

这是我的代码:

Write()方法:

# Appel cette méthode quand on modifie un enregistrement
    @api.multi
    def write(self, vals):
        result = super(ResPartnerSchool, self).write(vals)
        self.no_duplicate_school_dates()
        if not self.env.context.get('tracking_disable'):
            self.smart_synchronization()
        return result

smart_synchronization()方法:

@api.multi
def smart_synchronization(self):
    for record in self:
        if record.school_statut == "valid":
            record.create_compte_in_smart()
            if record.school_send_onde and record.school_date_status == fields.Date.today():
                record.prepare_files_for_onde()

当我使用此方法时,将调用prepar_files_to_ONDE方法:

# Méthode CRON pour fermetures contrats à une date saisie par l'agent
    @api.model
    def run_close_old_contracts(self):
        _logger.info("CRON Called for to verify closed dates contracts")
        today = fields.Date.today()
        domain = ['|', '|', ("half_pension_unsubscribe", "=", True),
                            ("nursery_morning_unsubscribe", "=", True),
                            ("nursery_evening_unsubscribe", "=", True)]
        for contract in self.search(domain):
            vals = {}
            if contract.school_registration >= today <= contract.school_end_date:
                if contract.half_pension_unsubscribe \
                        and contract.half_pension_unsubscribe_date <= today \
                        and contract.half_pension_status == "2":
                    vals["half_pension_status"] = "3"
                    _logger.info("Contract half pension closed for : " + contract.partner_id.name)
                if contract.nursery_morning_unsubscribe \
                        and contract.nursery_morning_unsubscribe_date <= today \
                        and contract.nursery_status_morning == "2":
                    vals["nursery_status_morning"] = "3"
                    _logger.info("Contract nursery morning closed for : " + contract.partner_id.name)
                if contract.nursery_evening_unsubscribe \
                        and contract.nursery_evening_unsubscribe_date <= today \
                        and contract.nursery_status_evening == "2":
                    vals["nursery_status_evening"] = "3"
                    _logger.info("Contract nursery evening closed for : " + contract.partner_id.name)
                if vals:
                    contract.write(vals)

在此级别上,我的write()方法的方法被调用。你有个主意吗?

谢谢

1 个答案:

答案 0 :(得分:0)

每次更新记录中的字段时,都会假设将调用ComponentWillReceiveProps函数。这是Odoo ORM内置的。

例如,如果我从数据库中获取一条记录。

write

然后我更新此伙伴记录上的所有字段,它将调用partner = env['res.partner'].browse(1) 函数来设置它们:

write

我强烈建议您仔细阅读核心存储库(# these each will call `write` at some point partner.name = 'abc' partner.update({'street1': 'My Street'}) )中class BaseModel定义中的基本ORM方法。遍历odoo/models.pyunlinkwrite_writeupdate等,可以使您更好地了解幕后情况。

当然,通读official ORM docs也不会有任何伤害。