Search code examples
postgresqlodoo-8ean-13

how to make ean13 unique in odoo8


I need to create a module in Odoov8 that can make ean13 field in product.template unique.

Here's my code:

# -*- 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 !')),
    ]

But it's not working! I'm working on this since yesterday


Solution

  • hey guys i dunno why _sql_constraints is not working but i tried something else and its working ! here's the code

    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 !!'))
    

    voila problem résolu merci