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

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
Discard

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

Author

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
Discard

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
Discard
Author

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 Views Activity
1
Apr 25
658
2
Jan 25
749
0
Sep 24
4
0
Sep 24
1113
1
Jun 24
1521