2016-03-01 63 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