2017-10-13 70 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