Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
2 Trả lời
10296 Lượt xem

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

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất
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
Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

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!")
Ảnh đại diện
Huỷ bỏ
Bài viết liên quan Trả lời Lượt xem Hoạt động
1
thg 1 22
18529
2
thg 8 19
8430
3
thg 7 19
5417
2
thg 12 21
7389
4
thg 11 20
7907