为什么我在 Odoo 中的 write() 方法没有设置值?

问题描述

我继承了一些模型。我还需要覆盖它的 write 方法

我已经试过了:

@api.multi
 def write(self,vals,context=None):
     res = super(WebSiteSupportTicket,self).write(vals)
     date = datetime.datetime.Now()
     if vals['state_id']:
         if vals['state_id'] == 7 or vals['state_id'] == 8:
             vals['closing_date'] = date
     print(vals)
     return res

其中 closure_date 是日期时间字段。

当我将 state_id 更改为 ID 为 7 或 8 的状态时,closure_date 仍然为空。但我知道代码正在通过 if 语句,因为我可以在 vals

的打印中看到 closure_date

我第一次遇到 write 方法的问题。为什么会发生,我如何获得解决方案?

解决方法

您在调用 closing_date 后将 super 添加到值字典中,closing_date 将不会被写入。

从函数定义中删除 context 参数(不需要)。您可以找到他们在帐户模块中覆盖发票 write 方法的示例。

示例

@api.multi
def write(self,values):
    # Define the closing_date in values before calling super
     if 'state_id' in values and values['state_id'] in (7,8) :
         values['closing_date'] = datetime.datetime.now()
    return super(WebSiteSupportTicket,self).write(values)