ValueError:预期单例:-Odoo v8

问题描述 投票:0回答:1

我有这个方法,它应该在

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 的问题。

python odoo odoo-8
1个回答
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'))
© www.soinside.com 2019 - 2024. All rights reserved.