콘텐츠로 건너뛰기
메뉴
커뮤니티에 참여하려면 회원 가입을 하시기 바랍니다.
신고된 질문입니다
2 답글
3550 화면

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?

아바타
취소
작성자 베스트 답변

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.

아바타
취소
베스트 답변

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

아바타
취소
작성자

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?

작성자

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

관련 게시물 답글 화면 활동
0
3월 25
1316
4
4월 24
174164
0
12월 23
2087
5
7월 25
227794
1
12월 22
3217