如何使ean13在odoo8中独树一帜



我需要在Odoov8中创建一个模块,使product.template中的ean13字段唯一。

这是我的代码:

# -*- coding: utf-8 -*-
from openerp import models, fields, api, _
from openerp.exceptions import ValidationError
class uniq_barcode(models.Model):
    inherit = "product.template"
    ean13 = fields.Char()
    _sql_constraints = [
        ('ean13_uniq', 'unique(ean13)', _('code bare exisite deja !')),
    ]

但它不起作用!我从昨天开始就在做这个

此代码将不会运行,因为您的模型不是从"product.template"继承的。您声明了inherit = "product.template",它应该是_inherit = "product.template",不要忘记_

嘿,伙计们,我不知道为什么_sql_contraints不起作用,但我尝试了其他方法,它起作用了!这是代码

class uni_barcode(models.Model):
_inherit = "product.product"

@api.one
@api.constrains('company_id', 'ean13', 'active')
def check_unique_company_and_ean13(self):
    if self.active and self.ean13 and self.company_id:
        filters = [('company_id', '=', self.company_id.id),
                   ('ean13', '=', self.ean13), ('active', '=', True)]
        prod_ids = self.search(filters)
        if len(prod_ids) > 1:
            raise Warning(
                _('Code bare existe deja !!'))

视觉问题解决方案商业

最新更新