【问题标题】:Why is my write() method in Odoo not setting the value?为什么我在 Odoo 中的 write() 方法没有设置值?
【发布时间】:2021-01-08 20:27:23
【问题描述】:

我继承了一些模型。我还需要重写它的 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 的状态时,closing_date 仍然为空。但我知道代码正在通过 if 语句,因为我可以在 vals

的打印上看到 closure_date

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

【问题讨论】:

    标签: python odoo odoo-12


    【解决方案1】:

    您在调用super 后将closing_date 添加到值字典中,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)
    

    【讨论】:

      猜你喜欢
      • 2014-01-03
      • 2019-09-10
      • 1970-01-01
      • 1970-01-01
      • 2015-08-07
      • 2011-02-14
      • 2011-10-21
      相关资源
      最近更新 更多