I have this method, which should loop on a One2many
object, but the actual loop isn't working, I mean, if I add just one line it works fine, but if I add more than one line, than it throws me the singleton
error:
@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'))
These are the two objects on which this method is based upon:
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
If I add more than one isbn
on bsi.production.order.lines
it throws me:
ValueError
Expected singleton: bsi.production.order.lines(10, 11)
Any ideas?
EDIT
The duplicate is a different situation, and actually I've changed my method to match the one explained in the other question, with no success. So it's not really, or at least not an api-only issue.
In your case, it's found more then one record set in order_lines and you tried to get isbn value from it.
Try with following code:
@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'))