Skip to Content
Menu
This question has been flagged
1 Reply
6688 Views

Hello,

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

I want to have in result : "iPod (16Go)" and iPod (32Go)".

I've tried two things but foreach, I haven't the result that I want.


First way :

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

products = productVariants.search([])

for rec in products:

     nom_produit = rec.name

     #Do something with the nom_produit

But this way gives me : "iPod" and "iPod" (without the variants but it gives me as many times the product that there are variants for this product)


The second way is :

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

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

    nom_produit = produits['name']

    #Do something with nom_produit

But this way gives me : "iPod" (without the variants and it gives me only once the product, even if there are variants for this product).


How can I get the name of the product and his variants please ?

Thanks for answer

Avatar
Discard
Best Answer

Hi Miadi,

To get the product variant name try the following code ,

Thank you

productVariants = self.env['product.product']
products = productVariants.search([])
for rec in products:
if rec.attribute_value_ids.name:
print "product with variants", rec.name + '(' + rec.attribute_value_ids.name + ')'
else:
print "product", rec.name
Avatar
Discard
Author

It doesn't work, but I found a solution :

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

products = productVariants.search([])

for rec in products:

nom_produit = rec.display_name

Hi , i missed that , we can do it easily by using rec.display name.

Also the above code will work fines. But this way is easiest

Author

Hi, Yes this way is easiest. I try your solution and it gives me an issue

what error you get from my solution ? i checked it, and it was working fine