Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
3 Trả lời
1988 Lượt xem

I am trying to add attributes to a product using XMLRPC, but I am quite struggling with how it all fits together.


My goal is as follows:

In the backend, I can use the "Add a line" on a product in the "Attributes & Variants" tab.


And I can then easily create both the attribute, and the value.


I want to do all that using XMLRPC, where of course I want to check if a certain attribute already exists on the product, or in general.


I have this code to check if an product.template.attribute.line​ exists, and try to create it, if not. However, it complains that I must set at least one value (but how?):


``` def get_attribute_line_id(self, product_template_id, attribute_id, value_id, create = True):

existing_line_ids = self.models.execute_kw(

self.database, self.uid, self.apikey, 'product.template.attribute.line', 'search', [[["value_ids.id", "=", value_id], ["attribute_id", "=", attribute_id], ["product_tmpl_id", "=", product_template_id]]])

def get_attribute_line_id(self, product_template_id, attribute_id, value_id, create = True):

existing_line_ids = self.models.execute_kw(

self.database, self.uid, self.apikey, 'product.template.attribute.line', 'search', [[["value_ids.id", "=", value_id], ["attribute_id", "=", attribute_id], ["product_tmpl_id", "=", product_template_id]]])


if create == True and len(existing_line_ids) == 0:

line_id = self.models.execute_kw(self.database, self.uid, self.apikey, 'product.template.attribute.line', 'create', [{'attribute_id': attribute_id, 'value_ids': [value_id], 'product_tmpl_id': product_template_id}])

existing_line_ids = [line_id]


return existing_line_ids


The value_id​was created in this method:

def get_attribute_line_id(self, product_template_id, attribute_id, value_id, create = True):

existing_line_ids = self.models.execute_kw(

self.database, self.uid, self.apikey, 'product.template.attribute.line', 'search', [[["value_ids.id", "=", value_id], ["attribute_id", "=", attribute_id], ["product_tmpl_id", "=", product_template_id]]])


if create == True and len(existing_line_ids) == 0:

line_id = self.models.execute_kw(self.database, self.uid, self.apikey, 'product.template.attribute.line', 'create', [{'attribute_id': attribute_id, 'value_ids': [value_id], 'product_tmpl_id': product_template_id}])

existing_line_ids = [line_id]


return existing_line_ids

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

Hiii,

Updated Working Code

Here's how your method should look:

def get_attribute_line_id ( self, product_template_id, attribute_id, value_id, create= True ): # Search for existing attribute line on the product existing_line_ids = self.models.execute_kw( self.database, self.uid, self.apikey, 'product.template.attribute.line' , 'search' , [[ [ 'product_tmpl_id' , '=' , product_template_id], [ 'attribute_id' , '=' , attribute_id], [ 'value_ids' , 'in' , [value_id]] ]] ) # If it doesn't exist and creation is allowed, create it if create and not existing_line_ids: line_id = self.models.execute_kw( self.database, self.uid, self.apikey, 'product.template.attribute.line' , 'create' , [{ 'product_tmpl_id' : product_template_id, 'attribute_id' : attribute_id, 'value_ids' : [( 6 , 0 , [value_id])] }] ) existing_line_ids = [line_id] return existing_line_ids

I hope it is of full use.

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

Hi, please check this: https://youtu.be/Cmbo2iGuTBY 
Hope it helps.

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

Why not configure the log file to show the XMLRPC calls Odoo makes via the Website and copy them?

https://www.odoo.com/forum/help-1/how-can-i-see-which-api-calls-are-made-to-learn-what-odoo-is-doing-at-specific-times-183911

Ảnh đại diện
Huỷ bỏ
Bài viết liên quan Trả lời Lượt xem Hoạt động
3
thg 7 25
1787
1
thg 7 24
2820
2
thg 10 23
2083
5
thg 5 24
16990
2
thg 3 24
2143