ValueError:预期单身: - Odoo v8

时间:2017-10-13 01:24:25

标签: python openerp odoo-8

我有这个方法,应该在One2many对象上循环,但是实际循环不起作用,我的意思是,如果我只添加一行它可以正常工作,但是如果我添加多行,它引发了singleton错误:

@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

如果我在isbn上添加了多个bsi.production.order.lines,则会抛出我:

ValueError

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

有什么想法吗?

修改

副本是一个不同的情况,实际上我已经改变了我的方法以匹配另一个问题中解释的方法,但没有成功。所以这不是真的,或者至少不是api唯一的问题。

1 个答案:

答案 0 :(得分: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.

相关问题