Skip to Content
Menu
This question has been flagged
2 Replies
4796 Zobrazenia

I am trying to create a Product in Inventory

id = models.execute_kw(db, uid, password, 'product.product', 'create', [{'name': "Green Ball"}])

I am looking for help to add product Attribute and Variants using external api.How the query would be build?

Like: 

Attribute:Color and Values:White



Avatar
Zrušiť

Did you ever get an answer or figure this out because the documentation regarding this is nonexistent or wrong everywhere I look

Autor

It's difficult to understand underlying architecture of oddo External API. I found documentation insufficient.

Best Answer

Hi,


To add product attributes and variants using the Odoo XML-RPC API, you need to understand the structure of the product attributes and variants in Odoo.
Here's how you can build the query to add a product with attributes and variants:


1. First, you need to create product attributes and attribute values if they don't exist. Then, you can link them to the product.
2. Create a product template and link the product attributes to it.
3. Create product variants based on the product template.
Here's a step-by-step guide on how to achieve this:# Define your product attributes and their values
attribute_values = [(0, 0, {'name': 'White'})]  # Adjust this according to your attribute values

# Create product attributes if they don't exist
attribute_id = models.execute_kw(db, uid, password, 'product.attribute', 'create', [{'name': 'Color'}])

# Create attribute values if they don't exist
attribute_value_ids = models.execute_kw(db, uid, password, 'product.attribute.value', 'create', attribute_values)

# Create a product template and link the product attributes to it
product_template_id = models.execute_kw(db, uid, password, 'product.template', 'create', [{
    'name': 'Green Ball',
    'attribute_line_ids': [(0, 0, {'attribute_id': attribute_id, 'value_ids': [(6, 0, attribute_value_ids)]})]
}])

# Create product variants based on the product template
product_variant_id = models.execute_kw(db, uid, password, 'product.product', 'create', [{
    'name': 'Green Ball - White',
    'product_tmpl_id': product_template_id,
    'attribute_value_ids': [(6, 0, attribute_value_ids)]
}])


Hope it helps

Avatar
Zrušiť

I think there is a mistake as for the last step, he should update (and not create) the product variants based on the product template (product variants are automatically created from product template)

Best Answer

You must use the model product.template.attribute.line, with relation to producs on the field product_id.


The problem you'll face, could be get the Attribute Id, cause the records are created by id, not by name. I created a method thats gets the ID from the name using the odoorpc package in python.

Avatar
Zrušiť
Autor

I am able to find support
https://www.odoo.com/forum/help-1/map-product-attribute-value-pair-using-odoo-api-201652
But 'value_ids':[(6,0,[2,4)] key-value pair is not comprehensible. Is there any support available?

Related Posts Replies Zobrazenia Aktivita
1
aug 25
398
1
aug 25
659
2
júl 25
3139
3
júl 25
795
1
jún 25
2313