تخطي للذهاب إلى المحتوى
القائمة
لقد تم الإبلاغ عن هذا السؤال
4 الردود
4397 أدوات العرض

Very new to Odoo with limited Python experience but trying to learn fast.  I tried to follow this post https://www.odoo.com/forum/help-1/question/can-i-enforce-unique-account-numbers-internal-reference-for-customers-and-vendors-140224 and this post https://www.odoo.com/forum/help-1/question/how-can-i-prevent-users-from-entering-duplicate-vendors-based-on-name-only-134963 and adapt it to products. 

I think the code works because if I try and change an existing Internal Reference to one that already exists, I get the correct error.   However, this code will also give an error on entering a new product even when the Internal Reference is unique. 

After a lot of playing of various combinations, my only thought is this due to the interaction of product.template and product.product and this kind of simple approach will not work.  Thanks in advance for any thoughts. 

On v13. 

Model:  Product Template

Trigger Condition:  On Creation & Update

Watched Fields:  default_code (Internal Reference)

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!")
الصورة الرمزية
إهمال

you may need to create one action for each of product.template.model and product.product (variant).

أفضل إجابة

Strange. I tried this in Odoo 13 and had the same problem as Jeff, but it worked OK in Odoo 12.

Later, after reading Dominique's comments I tried again in Odoo 13 and it was OK!!

I added one more line to the Python code so that a blank product reference is allowed:

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!")
الصورة الرمزية
إهمال
أفضل إجابة

This is very odd.. out of curiosity, I tried on my development machine (mac os), and it just works fine

Odoo 13

The code works:

- as an automated server action on product.template watching the field 'default_code'

- as a method with @api.constrains('default_code')

- without and with variant enabled


So I am completely unable to reproduce your error ...

الصورة الرمزية
إهمال
الكاتب

Chris, that worked! Not that I fully understand it but glad it works. Thanks for the help!

المنشورات ذات الصلة الردود أدوات العرض النشاط
2
يونيو 25
1567
0
مارس 25
1405
0
فبراير 25
1093
3
أغسطس 24
5784
1
أبريل 24
33