NeoVe
NeoVe

Reputation: 3907

Additional condition to function - Odoo v8

I have this method:

@api.multi
@api.constrains('order_lines', 'order_lines.qty', 'order_lines.isbn')
def check_quantity(self):
    for rec in self:
        if rec.order_lines:
            for line in rec.order_lines:
                if line.qty > line.isbn.qty_available:
                    raise Warning(('Quantity is invalid.'))
                if not line.isbn:
                    raise Warning(('Enter​ ​at least​ ​1​ ​ISBN to produce'))
                else:
                    self.write({'state': 'inprogress',},)

It functions perfectly for the qty_available conditional, and the inprogress state, but if I don't add any isbn which is a Many2one for product.product it doesn't throw any error, but doesn't works either.

It should raise the 'Enter​ ​at least​ ​1​ ​ISBN to produce' Warning, but it just loads and that's it.

If You need further explanation, please let me know.

Upvotes: 1

Views: 412

Answers (2)

khelili miliana
khelili miliana

Reputation: 3822

Try this, I make some modification, i use ValidationError instead Warning, and dont use @api.multi, the system will use @api.one when we use constraint, and there is a note in last line

from openerp.exceptions import ValidationError

#@api.constrains('order_lines', 'order_lines.qty', 'order_lines.isbn')
@api.one
def check_quantity(self):
    for line in self.order_lines:
        if line.isbn.qty_available and (line.qty > line.isbn.qty_available):
            raise ValidationError("Quantity is invalid. %s" % line.qty)
        if not line.isbn:
            raise ValidationError('Enter​ ​at least​ ​1​ ​ISBN to produce')
        else:
            self.write({'state': 'inprogress',},) # here you have used @api.multi, and this not correct 

Upvotes: 1

Charif DZ
Charif DZ

Reputation: 14751

don't use self inside the loop always remember that because if you call a method it will be applied on all the records inside it.

and don't call write inside @api.constrains or depends this will cause recursive error. because this two decorator are called in create or write method.

@api.multi
@api.constrains('order_lines', 'order_lines.qty', 'order_lines.isbn')
def check_quantity(self):
    for rec in self:
        if rec.order_lines:
            for line in rec.order_lines:
                if not line.isbn:
                    # and check for the product before the quantity
                    # because if there is no product line.isbn.qty_available 
                    # will raise exception
                    raise Warning(('Enter​ ​at least​ ​1​ ​ISBN to produce'))

                if line.qty > line.isbn.qty_available:
                    raise Warning(('Quantity is invalid.'))

                else:
                    # if you use self here you will update all records
                    # and don't use write in the constrains
                    # try to use update it may work if not you need to override write or create method
                    rec.update({'state': 'inprogress',},)
                # or if you are changing just one field
                # rec.state = 'inprogress'

Upvotes: 1

Related Questions