这是我的功能:
# Méthode CRON pour fermetures contrats
@api.model
def run_close_old_contracts(self):
_logger.info('CRON Called for to verify closed dates contracts')
domain = ['|', ('half_pension_unsubscribe_date', '<=', fields.Date.today()),
('nursery_morning_unsubscribe_date', '<=', fields.Date.today()),
('nursery_evening_unsubscribe_date', '<=', fields.Date.today())]
for contract in self.search(domain):
if contract.half_pension_unsubscribe_date <= fields.Date.today():
if contract.half_pension_status == "3":
contract.half_pension_status = "3"
if contract.nursery_morning_unsubscribe_date <= fields.Date.today():
if contract.nursery_status_morning == "3":
contract.nursery_status_morning = "3"
if contract.nursery_evening_unsubscribe_date <= fields.Date.today():
if contract.nursery_status_evening == "3":
contract.nursery_status_evening = "3"
因此,我使用CRON(自动操作)来调用此函数。
问题在于此函数针对每个填充条件以及所有找到的记录调用write()方法。
我想一次而不是每次输入条件时都调用一次write()方法。
我在考虑字典,但在这里看不到如何使用。
你有个主意吗?
谢谢!
答案 0 :(得分:1)
只需填写字典并在末尾write
:
@api.model
def run_close_old_contracts(self):
_logger.info('CRON Called for to verify closed dates contracts')
domain = ['|', ('half_pension_unsubscribe_date', '<=', fields.Date.today()),
('nursery_morning_unsubscribe_date', '<=', fields.Date.today()),
('nursery_evening_unsubscribe_date', '<=', fields.Date.today())]
for contract in self.search(domain):
to_write = {}
if contract.half_pension_unsubscribe_date <= fields.Date.today():
if contract.half_pension_status != "3":
to_write['half_pension_status'] = "3"
if contract.nursery_morning_unsubscribe_date <= fields.Date.today():
if contract.nursery_status_morning != "3":
to_write['nursery_status_morning'] = "3"
if contract.nursery_evening_unsubscribe_date <= fields.Date.today():
if contract.nursery_status_evening != "3":
to_write['nursery_status_evening'] = "3"
contract.write(to_write)
您将每个状态与“等于3”进行了比较,最后还写了“ 3”。那没有道理,因此我在上面的代码中将“等于”更改为“不等于”。
编辑:提示Odoo's documentation
在每个字段分配一个危险时,将触发数据库更新 同时设置多个字段或在多个字段上设置字段 记录(具有相同的值),请使用write()。