2016-03-01 62 views
0

我在GUI中创建一个字段Odoo有一个'compute'的方法。但我无法得到它。如何在GUI Odoo中的字段中定义计算?

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

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中的字段。

欢迎任何帮助,建议,建议。如果有人能帮助我,我将非常感激。

+1

你尽量避免for循环?也许尝试只删除它,并只使用条件。 –

+0

现在我收到以下错误: ValueError:“name'rec'未定义”评估 – beriliox

+0

当我说要避免循环时,当然需要修改其余的代码! –

回答

1

我不适用于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') 
+0

谢谢,但我得到同样的错误。 – beriliox

+1

它不能相同:rec在我的代码中没有更多。它不接受哪个价值? –

+0

第一个代码显示以下错误: ValueError:u中的禁止操作码“如果self.product_id.product_tmpl_id.type =='product',而不是self.niu:\ n self.niu = self.env [' next_by_code('sale.order.line')“ 而第二个代码显示以下错误: SyntaxError:'return'外部函数 – beriliox

0

逗人,

我已经遇到了类似的问题,你可以试试与

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')

1

解决方法是用字典式的赋值而不是自我赋值。注释,例如:

self.x_hora_estimada_llegada = self.date_order 

将抛出

forbidden opcode(s) in u....

但相反,您使用类似于字典的分配和你的领域会就好了!:

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