Skip to Content
Menu
This question has been flagged
2 Replies
4693 Views

Hello,


we have the barcode field as unique. When I archive a barcode, he don´t display the barcode in webfront, but is still in database. When I try to add a new product, with the same barcode, he trough an error:

IntegrityError: duplicate key value violates unique constraint "product_product_barcode_uniq"


Can I automatic delete the barcode, when I archiv a product?


Thank you

Avatar
Discard
Best Answer

Hi,

You can super the write function of the product.product model and check the value coming in the field named active, if the value in the field active is False, you can remove the value of the barcode.



@api.multi
def write(self, val):
    """Override default Odoo write function and extend."""
    if 'active' in vals and not vals['active']:
            vals.update({'barcode': ''})
    return super(ClassName, self).write(vals)

Thanks

Avatar
Discard
Author

Sorry .. first it seems working but did not delete the barcode.

did you filter the archived product and check still it contains the barcode ?

Author

I click on archiv .. then it is archived .. then I click again to set active ... and the barcode is still there

hope you have inherited the correct model, also a change is made in the code, please check, vals.update({'barcode': ''})

Author

I added the code to my import product module. changes added, but still the same.

Or did you know a way to get the product with the barcode xy?

prod = self.env['product.template'].search([('barcode', '=ilike', row['barcode']), ('active', '=', False)], limit=1)

this seems not working or don't get the product.

I have tested the above code for the odoo v12 and seems working for me. have you restarted the service after adding the code, hope you have added the code in the product model. for getting the product with the barcode XY use this,

prod = self.env['product.product'].search([('barcode', '=', 'XY')], limit=1)

There is a small change needed:
def write(self, vals):
"""Override default Odoo write function and extend."""
if 'active' in vals and not vals['active']:
vals.update({'barcode': False})
return super(ProductTemplate, self).write(vals)

I needed to change '' to False. Even though '' is falsy, Odoo doesn't allow two products to have '' as barcode.

Author Best Answer

Hi,


another similar question:

prod = self.env['product.template'].search([('default_code', '=ilike', row['internalReference'])], limit=1)
if not prod: # Check if exists and archived
prod = self.env['product.template'].search([('default_code', '=ilike', row['internalReference']),
('active', '=', False)], limit=1)

This wrote a freelancer ... why did he check extra if the product is active?

In the first query he also get the product, even when its archived ... right? 

So why again a second check?


and when I want first to check if the product with barcode "xy" exists, I use:

# check if product with barcode exists
prod = self.env['product.template'].search([('barcode', '=ilike', row['barcode'])], limit=1)
if not prod: # check if product with barcode exists and is not active
prod = self.env['product.template'].search([('barcode', '=ilike', row['barcode']),
('active', '=', False)], limit=1)
if not prod: # Check if exists
prod = self.env['product.template'].search([('default_code', '=ilike', row['internalReference'])], limit=1)
if not prod: # Check if exists and archived
prod = self.env['product.template'].search([('default_code', '=ilike', row['internalReference']),
('active', '=', False)], limit=1)

But the barcode is in product_product table .. did I get this with the query product.template?

Avatar
Discard