Odoo is the world's easiest all-in-one management software. It includes hundreds of business apps:
CRM | e-Commerce | Accounting | Inventory | PoS | Project management | MRP | etc.
How can be tested/checked (in an Odoo python method) if there is any product (product_product) with a given ean13 code?
I am populating EAN13 codes in a method and assigning them to a product_product item.
However, I would like to test in advance if value already exists for any other product.
How is tested in Odoo if there is any product_product with a given EAN13 value?
Or, as a more general question, how do you check in Odoo if there is any product with a certain value (say FOOBAR) in a given field?
You need to do a search on the model. Take an example
def check_ean13(self, cr, uid, ean13, context=None):
prod_count = self.pool.get('product.product').search(cr, uid, [('ean13','=', eanval)], count=True)
if prod_count > 0:
#product with the field ean13 and eanval exist
#there is no product with that ean13 value
You can also apply unique constraint for ean13, in case if you don't want generate duplicate ean13.
You can define unique constraint as below:
_sql_constraints = [
('ean_13_unique', 'unique (ean13)', 'The EAN13 code must be unique!')
Or if you want to use python method, check it as below:
def check_ean13(self, ean13):
for rec in self:
prod_recs = self.search([('ean13', '=', rec.ean13), ('id', '=', rec.id)], count=True)
if prod_recs > 0:
#process if duplicate ean13 exists for another product
#process if ean13 is unique.
Hope this helps!
As additional comment to this post, ean13 is part of product.product but it is populated in product.template. This is weird (IMHO) and there is even a note in the source code stating that ean13 population should be moved into product.product.
So if you are modifying create method it is normal that ean13 is not present in product.product create, as it is handled by product.template create.
Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!
About This Community
This platform is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.Register
Odoo Training Center
Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.Test it now
|Asked: 7/11/15, 3:36 AM|
|Seen: 525 times|
|Last updated: 7/12/15, 9:15 AM|