This question has been flagged
1 Reply
6011 Views

I have these models:

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

I'm trying to loop into bsi.production.order.lines and check all the 'isbn' which are products, this is what I've tried so far:

  1. @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'))

But If I add more than one product or 'isbn' to line, it throws me the 'Expected singleton' error.

How can I properly loop through bsi.production.order.lines?










Avatar
Discard
Best Answer

Hello Alberto,

If you want to do like this, without any lines production can't be start.
For that you need to try create and write method for this.

Example :-

from openerp import models, fields, api, _ from openerp.exceptions import UserError

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

    @api.model
    def create(self, vals):
        result = super(bsi_production_order, self).create(vals)
        if not result.order_lines:
            raise UserError(_('You Can not Start Production Without any Product.'))
        return result

    @api.multi
    def write(self, vals):
        result = super(WorkOrder, self).write(vals)
        if not self.order_lines:
            raise UserError(_('You Can not Start Production Without any Product.'))
        return result


Hope it will helps you.
Thanks,

Avatar
Discard
Author

Thank You Jignesh :) Awesome approach