Skip to Content
Menu
This question has been flagged
2 Replies
6221 Views

Hello,

In a module that I've created (class miadi_packaging), I want to do take the name of a product which is in the product.product table.

I'm trying two ways but I've an error with the both.

First I tried this :

produits = self._cr.execute("SELECT * FROM product_product")

for produits in self.env.cr.fetchall():

    nom_produit = produits['display_name']

    #Doing something with the nom_produit
But it tell me that tuple indices must be integers, not str and if I write "SELECT display_name FROM product_product", it tell me that the display_name column doesn't exist.


In the second way, I tried to do this :

productVariants = self.env['product.product']

products = productVariants.search([])

and it gives me something like this : product_product(7, 18, 25, 36, 38, 39, 75...)

How can I take the name of ONE product at a time with this result ?


Thanks for help

Avatar
Discard
Best Answer

Hi Miadi,

You can get the names of the products like this

productVariants = self.env['product.product']

products = productVariants.search([])

for rec in products:

     print "product name ..", rec.name

cr.execute("select * from product_product")
products = cr.dictfetchall()
for rec in products:
    print rec['name']
Avatar
Discard
Author

Hi, the first way works but how can I take the variants in parentheses like this : "iPod (16Go)" because actually, I have two "iPod" but I want to have "iPod (16Go)" and "iPod (32 Go)" ?

Hi,

Just select the data from product_template then, hope you will get it

Author

Hi,

Can you give an example ?

Because if I take only the name from product_template, I've one product named "iPod" while I've two iPod, and if I take the name from product_product, I've two "iPod" but no the variants

Regards

Hi,

products = self._cr.execute("SELECT name FROM product_template")

for products in self.env.cr.dictfetchall():

print products['name']

Author

It's what I said, in the product table, I've two iPod, one with 16Go and one with 32Go and I want in result "iPod(16Go)" and "iPod(32Go)" but with your solution, I've only one result, it's "iPod"

Hi MIADI, I believe you made the product with variants. The template product (iPod) is on the table product_template. The other ones (iPod(16Go) and iPod(32Go)) can be found on the table product_product.

Author

Hi Yenthe, you've good, it's exactly what I made. But if I take the name in product_product, the result is : "iPod" and "iPod", there are not the variants but what I want is : "iPod (16Go)" and iPod (32Go)". How do it ?

Author

Sorry for the previous comment...

So I've made this code :

productVariants = self.env['product.product']

products = productVariants.search([])

for rec in products:

nom_produit = rec.name

#Doing something with nom_produit

and it gives me : "iPod" and "iPod".

How can I modify my code to have "iPod (16Go)" and "iPod (32Go)"

Author Best Answer

Hi,

Thanks for your answer, the first way works fine but I didn't try the second way.

Thanks,

Regards,

MIADI

Avatar
Discard