【问题标题】:How to define a compute in a field in the GUI Odoo?如何在 GUI Odoo 的字段中定义计算?
【发布时间】:2017-01-22 01:51:24
【问题描述】:

我正在 GUI 中创建一个字段 Odoo 有一个带有“计算”的方法。但我无法得到它。

我在 sale.py 模块的 sale.order.line 中创建了一个使用计算属性的字段。

niu = fields.Char(string="NIU", compute="_niu_validation", readonly=True, store=True)

@api.depends('product_id.product_tmpl_id.type')
def _niu_validation(self):
    for rec in self:
        if rec.product_id.product_tmpl_id.type == 'product' and not rec.niu:
                rec.niu = self.env['ir.sequence'].next_by_code('sale.order.line')

这很完美,但在 GUI Odoo 中也是如此。

图片下方显示:http://es.zimagez.com/zimage/computefield.php

但它显示了以下错误:

ValueError: forbidden opcode(s) in u"for rec in self:\n        if rec.product_id.product_tmpl_id.type == 'product' and not rec.niu:\n \t    rec.niu = self.env['ir.sequence'].next_by_code('sale.order.line')"

可能存在语法错误,但我不知道如何在 GUI Odoo 中为字段定义方法。

欢迎任何帮助、建议、建议。如果有人可以帮助我,我将不胜感激。

【问题讨论】:

  • 您是否尝试避免 for 循环?也许尝试将其删除并仅在条件下使用。
  • 现在我收到以下错误:ValueError: "name 'rec' is not defined" while evaluation
  • 当我说要避免循环时,您当然需要调整其余代码!

标签: python openerp odoo-9


【解决方案1】:

解决方案是使用类似字典的赋值而不是 self.注释,例如:

 self.x_hora_estimada_llegada = self.date_order

会扔

u....中禁止的操作码

但是你使用类似字典的赋值,你的字段就可以了!:

for record in self:
    record['x_hora_estimada_llegada'] = self.date_order

【讨论】:

    【解决方案2】:

    我不使用 v9,所以我认为您只需要进行一些试验和错误。

    试试这个:

        if self.product_id.product_tmpl_id.type == 'product' and not self.niu:
                self.niu = self.env['ir.sequence'].next_by_code('sale.order.line')
    

    如果还是不行,可以试试:

        if self.product_id.product_tmpl_id.type == 'product' and not self.niu:
                return self.env['ir.sequence'].next_by_code('sale.order.line')
    

    【讨论】:

    • 不可能相同:rec 不再出现在我的代码中。它不接受哪个值?
    • 第一个代码显示如下错误:ValueError: disabled opcode(s) in u"if self.product_id.product_tmpl_id.type == 'product' and not self.niu:\n self.niu = self.env['ir.sequence'].next_by_code('sale.order.line')" 第二个代码显示以下错误: SyntaxError: 'return' outside function
    • 这没什么用。在缺乏文档的情况下,我会做的是放置断点并逐步调试 Calcular 字段的解析器,以了解您可以在其中放置什么,以及您不能在其中放置什么
    【解决方案3】:

    亲爱的,

    我也遇到过类似的问题,你可以试试

    如果 self.product_id.product_tmpl_id.type == 'product' 而不是 self.niu: self.['niu'] = self.env['ir.sequence'].next_by_code('sale.order.line')

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多