Odoo v8功能的附加条件

2024-04-25 16:38:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我有这个方法:

@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',},)

对于qty_available条件和inprogress状态,它的功能是完美的,但是如果我不添加任何isbn,它是product.productMany2one,它不会抛出任何错误,但也不会起作用。在

它应该发出'Enter​ ​at least​ ​1​ ​ISBN to produce'警告,但它只是加载,仅此而已。在

如果你需要进一步的解释,请告诉我。在


Tags: inselfapiforiflineorderavailable
2条回答

试试这个,我做了一些修改,我用ValidationError代替Warning,并且不要使用@api.multi,当我们使用constraint时,系统将使用@api.one,最后一行有一个注释

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 

不要在循环中使用self,因为如果调用 一种方法,它将应用于其中的所有记录。在

不要在@api.constrainsdepends内调用write,这将导致递归错误。因为这两个修饰符是在createwrite方法中调用的。在

@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'

相关问题 更多 >