Odoo Help

Welcome!

This community 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.

0

How can be tested/checked (in an Odoo python method) if there is any product (product_product) with a given ean13 code?

By
E.M.
on 7/11/15, 3:36 AM 471 views

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? 

2

Axel Mendoza

--Axel Mendoza--
10228
| 7 7 8
Camaguey, Cuba
--Axel Mendoza--

DevOps - Full stack - Software Architect - Developer - Technology Integrator

I could help you to develop anything and solve complex problems based on technologies, integrations and tricky stuffs mostly in Python with OpenERP/Odoo, Zato, Django and many others frameworks programming languages and technologies.

I offers consulting services to anyone with an unanswered questions or needs for customizations. Think about it, maybe it's better to have an expert to solve your issues and projects than having a full time employee trying to understand what to do an how

Reach me at aekroft@gmail.com

Axel Mendoza
On 7/11/15, 4:57 PM

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
else:
#there is no product with that ean13 value

But if I try that within a create method, 'cr' and 'uid' are not defined, right? Are they needed for the search method?

E.M.
on 7/11/15, 8:01 PM

That depends on what api style you are using my example was using old api that still works. But the same apply without cr and uid for the new api

Axel Mendoza
on 7/11/15, 8:08 PM

Thanks Axel, a nice chap posted this url https://www.odoo.com/documentation/8.0/reference/orm.html#porting-from-the-old-api-to-the-new-api for other question I made. That url helps to understand both APIs, strongly recommended for those of us who are struggling to understand the basics in Odoo.

E.M.
on 7/12/15, 5:23 AM
1
Kalpana Hemnani
On 7/12/15, 5:18 AM

Hello,

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:

@api.multi

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

        else:

            #process if ean13 is unique.

Hope this helps!

Thanks,

Kalpana Hemnani

Thanks for the side note Kalpana. This is helpful information and I am sure it will be useful in the future, in this specific case I don't want to enforce it across all code, just in one method. Now I am wondering how to retrieve ean13 code given a product.product ID. I am sure is easy once you know where to look for it.

E.M.
on 7/12/15, 5:20 AM
0
E.M.
On 7/12/15, 9:15 AM

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.

Your Answer

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 community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

2 follower(s)

Stats

Asked: 7/11/15, 3:36 AM
Seen: 471 times
Last updated: 7/12/15, 9:15 AM