Skip ke Konten
Menu
Pertanyaan ini telah diberikan tanda
1 Balas
6817 Tampilan

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
Buang
Jawaban Terbai

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
Buang
Penulis

Thank You Jignesh :) Awesome approach

Post Terkait Replies Tampilan Aktivitas
1
Mar 25
680
0
Jul 25
312
2
Jun 25
457
1
Mei 25
697
0
Mar 25
987