0

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

Sofiane Mdj
  • 63
  • 2
  • 10

2 Answers2

0

This code will not run because your model did not inherit from 'product.template'. You declared inherit = "product.template", it should be _inherit = "product.template", don't forget _

Hung Hoang
  • 697
  • 5
  • 14
0

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

Sofiane Mdj
  • 63
  • 2
  • 10