0
votes

i have developed a module of purchase in odoo 10 community when i make a cammand of products i want to disable duplicate choose of product in widget one2many list as in the image below: enter image description here

i want to prevent duplicate entry in the products list here is the code of my command module:

class PalBl(models.Model):
        _name = 'pal.bl'
        name = fields.Char('Reference', required=True)
        supplier = fields.Many2one('pal.vendor', required=True)
        date = fields.Date('Date', required=True)
        totalHt = fields.Float('Total HT', store=True, readonly=True, compute='_get_tot')
        totalTtc = fields.Float('Total TTC', store=True, readonly=True, compute='_get_tot')
        items_id = fields.One2many('pal.prs.com', 'prod_id')
        dateliv = fields.Date('Date de livraison prévue')
        nb_pr = fields.Integer('Total de Produit')
        state = fields.Selection([(1, 'En attente'), (2, 'Reglée')], 'type', default=1)
        _sql_constraints = [('item_code_uniq', 'unique(items_id.name.code)', "le code d'un produit doit etre unique !")]

and this the code of the products:

    class PalPrcom(models.Model):
            _name = 'pal.prs.com'
            name = fields.Many2one('pal.stock', 'Désignation', required=True)
            code = fields.Char('Ref produit', store=True, readonly=True, compute='_getref', inverse='_gedef')
            quantity = fields.Integer('Quantité', required=True, default=1)
            price = fields.Float('Prix achat HT', store=True, readonly=True, compute='_getref')
            tva = fields.Integer('TVA')
            remise = fields.Integer('Remise')
            prod_id = fields.Many2one('pal.bl')
            _sql_constraints = [ ('quantity_gt_zero', 'CHECK (quantity>0)', 'La quantité de produit doit etre supérieur à zéro!')

]
2

2 Answers

0
votes

You can use two for loops which will iterate over your one2many field and check for duplicates

0
votes

This will work.

_sql_constraints = [('order_name', 'unique (relation_id,field_name_1,field_name_2)',
                 'Duplicates are not allowed!')]