Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
1 Trả lời
6943 Lượt xem

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?










Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

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,

Ảnh đại diện
Huỷ bỏ
Tác giả

Thank You Jignesh :) Awesome approach

Bài viết liên quan Trả lời Lượt xem Hoạt động
1
thg 3 25
850
0
thg 7 25
354
2
thg 6 25
1283
1
thg 5 25
1307
0
thg 3 25
1159