Skip to Content
Odoo Menú
  • Registra entrada
  • Prova-ho gratis
  • Aplicacions
    Finances
    • Comptabilitat
    • Facturació
    • Despeses
    • Full de càlcul (IA)
    • Documents
    • Signatura
    Vendes
    • CRM
    • Vendes
    • Punt de venda per a botigues
    • Punt de venda per a restaurants
    • Subscripcions
    • Lloguer
    Imatges de llocs web
    • Creació de llocs web
    • Comerç electrònic
    • Blog
    • Fòrum
    • Xat en directe
    • Aprenentatge en línia
    Cadena de subministrament
    • Inventari
    • Fabricació
    • PLM
    • Compres
    • Manteniment
    • Qualitat
    Recursos humans
    • Empleats
    • Reclutament
    • Absències
    • Avaluacions
    • Recomanacions
    • Flota
    Màrqueting
    • Màrqueting Social
    • Màrqueting per correu electrònic
    • Màrqueting per SMS
    • Esdeveniments
    • Automatització del màrqueting
    • Enquestes
    Serveis
    • Projectes
    • Fulls d'hores
    • Servei de camp
    • Suport
    • Planificació
    • Cites
    Productivitat
    • Converses
    • Validacions
    • IoT
    • VoIP
    • Coneixements
    • WhatsApp
    Aplicacions de tercers Odoo Studio Plataforma d'Odoo al núvol
  • Sectors
    Comerç al detall
    • Llibreria
    • Botiga de roba
    • Botiga de mobles
    • Botiga d'ultramarins
    • Ferreteria
    • Botiga de joguines
    Food & Hospitality
    • Bar i pub
    • Restaurant
    • Menjar ràpid
    • Guest House
    • Distribuïdor de begudes
    • Hotel
    Immobiliari
    • Agència immobiliària
    • Estudi d'arquitectura
    • Construcció
    • Gestió immobiliària
    • Jardineria
    • Associació de propietaris de béns immobles
    Consultoria
    • Empresa comptable
    • Partner d'Odoo
    • Agència de màrqueting
    • Bufet d'advocats
    • Captació de talent
    • Auditoria i certificació
    Fabricació
    • Textile
    • Metal
    • Mobles
    • Menjar
    • Brewery
    • Regals corporatius
    Salut i fitness
    • Club d'esport
    • Òptica
    • Centre de fitness
    • Especialistes en benestar
    • Farmàcia
    • Perruqueria
    Trades
    • Servei de manteniment
    • Hardware i suport informàtic
    • Sistemes d'energia solar
    • Shoe Maker
    • Serveis de neteja
    • Instal·lacions HVAC
    Altres
    • Nonprofit Organization
    • Agència del medi ambient
    • Lloguer de panells publicitaris
    • Fotografia
    • Lloguer de bicicletes
    • Distribuïdors de programari
    Browse all Industries
  • Comunitat
    Aprèn
    • Tutorials
    • Documentació
    • Certificacions
    • Formació
    • Blog
    • Pòdcast
    Potenciar l'educació
    • Programa educatiu
    • Scale-Up! El joc empresarial
    • Visita Odoo
    Obtindre el programari
    • Descarregar
    • Comparar edicions
    • Novetats de les versions
    Col·laborar
    • GitHub
    • Fòrum
    • Esdeveniments
    • Traduccions
    • Converteix-te en partner
    • Services for Partners
    • Registra la teva empresa comptable
    Obtindre els serveis
    • Troba un partner
    • Troba un comptable
    • Contacta amb un expert
    • Serveis d'implementació
    • Referències del client
    • Suport
    • Actualitzacions
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Programar una demo
  • Preus
  • Ajuda

Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:

  • CRM
  • e-Commerce
  • Comptabilitat
  • Inventari
  • PoS
  • Projectes
  • MRP
All apps
You need to be registered to interact with the community.
All Posts People Badges
Etiquetes (View all)
odoo accounting v14 pos v15
About this forum
You need to be registered to interact with the community.
All Posts People Badges
Etiquetes (View all)
odoo accounting v14 pos v15
About this forum
Ajuda

Get product's concatenated attributes in python model

Subscriure's

Get notified when there's activity on this post

This question has been flagged
pythoncoding
2 Respostes
4441 Vistes
Avatar
Parker D

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?

0
Avatar
Descartar
Avatar
Parker D
Autor 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.

0
Avatar
Descartar
Avatar
Cybrosys Techno Solutions Pvt.Ltd
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

0
Avatar
Descartar
Parker D
Autor

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?

Parker D
Autor

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

Enjoying the discussion? Don't just read, join in!

Create an account today to enjoy exclusive features and engage with our awesome community!

Registrar-se
Related Posts Respostes Vistes Activitat
new python env
python
Avatar
0
de març 25
2468
What means "Too many values to unpack" message? Solved
python
Avatar
Avatar
Avatar
Avatar
Avatar
4
d’abr. 24
176133
have no data in screen. read data in my own module from different model
python
Avatar
0
de des. 23
3086
How to insert value to a one2many field in table with create method? Solved
python
Avatar
Avatar
Avatar
Avatar
Avatar
5
de jul. 25
232723
how to disable add product in sales of odoo 12
python
Avatar
Avatar
1
de des. 22
4283
Community
  • Tutorials
  • Documentació
  • Fòrum
Codi obert
  • Descarregar
  • GitHub
  • Runbot
  • Traduccions
Serveis
  • Allotjament a Odoo.sh
  • Suport
  • Actualització
  • Desenvolupaments personalitzats
  • Educació
  • Troba un comptable
  • Troba un partner
  • Converteix-te en partner
Sobre nosaltres
  • La nostra empresa
  • Actius de marca
  • Contacta amb nosaltres
  • Llocs de treball
  • Esdeveniments
  • Pòdcast
  • Blog
  • Clients
  • Informació legal • Privacitat
  • Seguretat
الْعَرَبيّة Català 简体中文 繁體中文 (台灣) Čeština Dansk Nederlands English Suomi Français Deutsch हिंदी Bahasa Indonesia Italiano 日本語 한국어 (KR) Lietuvių kalba Język polski Português (BR) română русский язык Slovenský jazyk slovenščina Español (América Latina) Español ภาษาไทย Türkçe українська Tiếng Việt

Odoo és un conjunt d'aplicacions empresarials de codi obert que cobreix totes les necessitats de la teva empresa: CRM, comerç electrònic, comptabilitat, inventari, punt de venda, gestió de projectes, etc.

La proposta única de valor d'Odoo és ser molt fàcil d'utilitzar i estar totalment integrat, ambdues alhora.

Website made with

Odoo Experience on YouTube

1. Use the live chat to ask your questions.
2. The operator answers within a few minutes.

Live support on Youtube
Watch now