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

Hello there :)

I have a little custom made app that exports product variant's data to a CSV file and now, I'd like to add their attributes. When I export product variants directly through Odoo and I add the column "product_template_attribute_value_ids", I get the attribute names and values as a text, for example "Size: XXL, Color: red" for a t-shirt.

Unfortunately I do not know how to get this data through my script. 

Here is a simplified version of my script, including my latest try to get the attributes:

with tempfile.NamedTemporaryFile(prefix="product data", suffix=".csv") as data_file:
headers = ['ID', 'Internal Reference', 'Name', 'German Name', 'Attributes', 'Attributes German','GTIN / EAN']
writer = pycompat.csv_writer(data_file, quoting=1)
writer.writerow(headers)
product_ids = product_obj.search([])
for product in product_ids:
english_product_name = product.with_context({'lang': 'en_US'}).name
german_product_name = product.with_context({'lang': 'de_DE'}).name

# trying to get the attributes starts here
english_attributes = ''
german_attributes = ''
for attributes in product.attribute_line_ids:
for attribute_values in attributes.product_template_value_ids:
english_attributes += attribute_values.with_context({'lang': 'en_US'}) + ','
german_attributes += attribute_values.with_context({'lang': 'de_DE'}) + ','
# trying to get the attributes ends here

writer.writerow(
[product.id, product.default_code, english_product_name, german_product_name, english_attributes, german_attributes, product.barcode])

This does not work and instead of the actual attributes of the particular variant, this lists all the attribute's values of the product's template, e.g. "S,L,XL,XXL,XXXL,blue,red,green,black,yellow,". 

Does anyone know how to get the attribute's names and value for each product's variant?

Avatar
Discard
Author Best Answer

I've finally found it! 😁
The pre-made string can be found in "product_template_attribute_value_ids" of the product.product data model. This relates to this model "product.template.attribute.value", which has the desired string in the column "display_name". 
Here is my adjusted code: 

   # trying to get the attributes starts here
english_attributes = ''
german_attributes = ''
eng_attrs = []
de_attrs = []
 for attribute in product.product_template_attribute_value_ids:
    eng_attrs.append(attribute.with_context({'lang': 'en_US'}).display_name)
de_attrs.append(attribute.with_context({'lang': 'de_DE'}).display_name)
english_attributes = ', '.join(eng_attrs)
german_attributes = ', '.join(de_attrs)
# trying to get the attributes ends here

This delivers the exact string I was looking for. The yellow t-shirt in XXL has the string "Size: XXL, Color: yellow" in English and the correct translation in German. 

Thanks again, @Cybrosis.

Avatar
Discard
Best Answer

Hi,

Try the following code. I have changed some of your variable names.

for product in product_ids:
english_product_name = product.with_context({'lang': 'en_US'}).name
german_product_name = product.with_context({'lang': 'de_DE'}).name

# trying to get the attributes starts here
english_attributes = ''
german_attributes = ''
eng_attrs = []
de_attrs = []
for attribute in product.attribute_line_ids:
for attribute_value in attribute.product_template_value_ids:
eng_attrs.append(attribute_value.attribute_id.with_context(
{'lang': 'en_US'}).name + ': ' + attribute_value.with_context(
{'lang': 'en_US'}).name)
de_attrs.append(attribute_value.attribute_id.with_context(
{'lang': 'de_DE'}).name + ': ' + attribute_value.with_context(
{'lang': 'de_DE'}).name)
english_attributes = ', '.join(eng_attrs)
german_attributes = ', '.join(de_attrs)
# trying to get the attributes ends here

Regards

Avatar
Discard
Author

Hi Cybrosys and thank you! But unfortunately this also adds all possible attribute values of the product template as well, not those of the variant.

Instead of this: "S,L,XL,XXL,XXXL,blue,red,green,black,yellow,"

I now get this: "Size: S, Size: L, Size: XL, Size: XXL, Size: XXXL, Color: blue, Color: red, Color: green [...]".

I guess, I could adjust it, so the attribute name would only appear once, but I still have the problem to narrow the attributes and their values down to those used on the individual variant.
Any ideas?

Author

I forgot to mention, that I use Odoo 14 Enterprise.

Related Posts Replies Views Activity
4
Apr 24
170719
0
Dec 23
602
5
Nov 24
217114
1
Dec 22
1692
2
Nov 22
1678