Skip to Content
Menú
This question has been flagged
2 Respostes
10291 Vistes

hello guys so i have 

model product identity

field:

1. ~~

2. ~~

3. barcode 


and product template has relation one2many to product identity

and i want to give/raise warning every product identity created in one2many tree view when there is record with the same/duplicate barcode in the one2many data.. 


can anyone help me? i try use api.onchange but it seems not that easy thank you :D

Avatar
Descartar
Best Answer
Consider identity_ids as one2many field in the product template
You can write an onchange function for this field.

@api.onchange('identity_ids')
def onchange_identity_ids(self):
for order in self.identity_ids:
line = self.identity_ids.filtered(lambda l: l.barcode == order.barcode)
if len(line) > 1:
raise ValidationError(_('You cannot have multiple lines with same barcode(%s)')%(line[1].barcode))
break
Avatar
Descartar
Best Answer

Here's an example of using an Automated Action to prevent duplicates (Odoo already blocks duplicate barcodes so this is the Internal Reference).  If I understand your requirement you could change the Python code to ensure that there are no duplicates within the product identity group.

https://odootricks.tips/automated-action-to-prevent-duplicates/


Model:  Product Template
Action To Do:    Execute Python Code
Trigger Condition:   On Creation & Update
Before Update Domain: Internal Reference is set
Apply on:  Match all records 

Python Code

if record.default_code:
   existing_product = env['product.template'].search([('id','!=',record.id),('default_code','=',record.default_code)])
   if existing_product:
     raise Warning("You can't have the same Internal Reference Number in Odoo twice!")
Avatar
Descartar
Related Posts Respostes Vistes Activitat
1
de gen. 22
18526
2
d’ag. 19
8430
3
de jul. 19
5417
2
de des. 21
7388
4
de nov. 20
7906