2017-10-13 58 views
2

我有這種方法,它應該循環在One2many對象,但實際循環不工作,我的意思是,如果我只添加一條線,它工作正常,但如果我添加多行,比它拋出我的singleton錯誤:ValueError:期望的單身人士: - Odoo v8

@api.multi 
@api.depends('order_lines', 'order_lines.isbn') 
def checkit(self): 
    for record in self: 
     if self.order_lines.isbn: 
      return self.order_lines.isbn 
     else: 
      raise Warning(('Enter​ ​at least​ ​1​ ​ISBN to produce')) 

這些都是此方法是基於兩個對象:

class bsi_production_order(models.Model): 
    _name = 'bsi.production.order' 

    name = fields.Char('Reference', required=True, index=True, copy=False, readonly='True', default='New') 
    date = fields.Date(string="Production Date") 
    production_type = fields.Selection([ 
    ('budgeted','Budgeted'), 
    ('nonbudgeted','Non Budgeted'), 
    ('direct','Direct Order'), 
], string='Type of Order', index=True, 
track_visibility='onchange', copy=False, 
help=" ") 
    notes = fields.Text(string="Notes") 
    order_lines = fields.One2many('bsi.production.order.lines', 'production_order', states={'finished': [('readonly', True)], 'cancel': [('readonly', True)]}, string="Order lines", copy=True) 

class bsi_production_order_lines(models.Model): 
    _name = 'bsi.production.order.lines' 

    production_order = fields.Many2one('bsi.production.order', string="Production Orders") 
    isbn = fields.Many2one('product.product', string="ISBN", domain="[('is_isbn', '=', True)]") 
    qty = fields.Integer(string="Quantity") 
    consumed_qty = fields.Float(string="Consumed quantity") 
    remaining_qty = fields.Float(string="Remaining quantity", compute="_remaining_func") 

    @api.onchange('qty', 'consumed_qty') 
    def _remaining_func(self): 
     if self.consumed_qty or self.qty: 
      self.remaining_qty = self.consumed_qty - self.qty 

如果我添加多個isbnbsi.production.order.lines它拋出我:

ValueError 

Expected singleton: bsi.production.order.lines(10, 11) 

任何想法?

編輯

重複的是另一種情況,實際上我已經改變了我的方法來匹配一個在其他問題中詳細說明,但沒有成功。所以這不是真的,或者至少不是一個只有api的問題。

+0

可能重複[ValueError Expected singleton,Odoo8](https://stackoverflow.com/questions/31070640/valueerror-expected-singleton-odoo8) –

回答

3

在你的情況下,它的發現不止一個紀錄ORDER_LINES設定時,你想從它那裏得到ISBN值。

嘗試用下面的代碼:

@api.multi 
@api.depends('order_lines', 'order_lines.isbn') 
def checkit(self): 
    for record in self: 
     if record.order_lines: 
      for line in record.order_lines: 
       if line.isbn: 
        return line.isbn 
     else: 
      raise Warning(('Enter​ ​at least​ ​1​ ​ISBN to produce')) 

對於這些錯誤的詳細信息。你可以參考我的blog.

+0

超棒的,謝謝你,會檢查你的博客,乾杯 – NeoVe