2016-07-25 10 views
0

Ich muss ein Modul in Odoov8 erstellen, das Feld ean13 in product.template einzigartig machen kann.wie man ean13 einzigartig in odoo8 macht

Hier ist mein 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 !')), 
    ] 

Aber es funktioniert nicht! Ich arbeite seit gestern daran

+0

Warum gehst du nicht einfach einen eindeutigen Index für die Spalte erstellen Résolu? –

Antwort

0

hey Jungs, ich weiß nicht, warum _sql_constraints nicht funktioniert, aber ich versuchte, etwas anderes und seine Arbeits! hier ist der 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 merci

0

Dieser Code wird nicht ausgeführt, da Ihr Modell nicht von "product.template" geerbt hat. Sie erklärt inherit = "product.template", es _inherit = "product.template" sein sollte, nicht vergessen _

+0

Ich überprüfe das: D –

+0

gut tatsächlich in meinem Code war es mit _inherit immer noch nicht funktioniert –