简体   繁体   English

如何使用 XMLRPC 在 Odoo 中写入字段 many2one

[英]How to write fields many2one in Odoo with XMLRPC

I want to use the xmlrpc for the model "product.category".我想将 xmlrpc 用于 model“product.category”。

How to write the field "categ_id" with "read and search"in PYTHON? PYTHON中的“categ_id”字段和“read and search”怎么写? I have always an error.我总是出错。 I need to do that because i have to transfer this informations into another database Odoo.我需要这样做,因为我必须将这些信息传输到另一个数据库 Odoo。

    import xmlrpc.client
    import time
    url_db1="http://localhost:8069"
    db_1='DB_ONE'
    username_db_1='username'
    password_db_1='password'

    common_1 = xmlrpc.client.ServerProxy('{}/xmlrpc/2/common'.format(url_db1))
    models_1 = xmlrpc.client.ServerProxy('{}/xmlrpc/2/object'.format(url_db1))
    version_db1 = common_1.version()

    print("details..", version_db1)


    url_db2="http://localhost:806972"
    db_2='DB_2'
    username_db_2='myemail'
    password_db_2='mypassword'

    common_2 = xmlrpc.client.ServerProxy('{}/xmlrpc/2/common'.format(url_db2))
    models_2 = xmlrpc.client.ServerProxy('{}/xmlrpc/2/object'.format(url_db2))
    version_db2 = common_2.version()

    print("details..", version_db2)


    uid_db1 = common_1.authenticate(db_1, username_db_1, password_db_1, {})
    uid_db2 = common_2.authenticate(db_2, username_db_2, password_db_2, {})

    db_1_categories = models_1.execute_kw(db_1, uid_db1, password_db_1, 'product.category', 'search_read', [[]], {'fields':['id','name', 'parent_id']})

    total_count = 0
    for category in db_1_categories:
        print("category..", category)
        total_count +=1
        values = {}
        values['id'] = category['id']
        values['name'] = category['name']
        if category['parent_id']:
            values['parent_id'] = category['parent_id'][0]
        new_lead = models_2.execute_kw(db_2, uid_db2, password_db_2, 'product.category', 'create', [values])
print("Total Created..", total_count)

This is the error: ValidationError: ('The operation cannot be completed: another model requires the record being deleted. If possible, archive it instead.\n\nModel: Product Category (product.category), Constraint: product_category_parent_id_fkey', None)\n'>这是错误:ValidationError: ('操作无法完成:另一个 model 需要删除记录。如果可能,请将其存档。\n\n模型:产品类别 (product.category),约束:product_category_parent_id_fkey',无) \n'>

在此处输入图像描述

The method name should be search_read方法名称应为search_read

Example :示例

models.execute_kw(db, uid, password,
   'product.template', 'search_read',
    [],
    {'fields': ['name', 'default_code', 'categ_id']})

It should return a list of dicts, the categ_id field value is a list of two values, the first is the id of the category and the second is the name of the category.它应该返回一个字典列表, categ_id字段值是一个包含两个值的列表,第一个是类别的 id,第二个是类别的名称。

To write categ_id , you just need to provide the category ID to the write method.编写categ_id ,只需将类别 ID 提供给 write 方法即可。

Example:例子:

product_template_data = [{'default_code': 'FURN_6666', 'categ_id': [8, 'All / Saleable / Office Furniture'], 'id': 23, 'name': 'Acoustic Bloc Screens'}, ...] 

for ptd in product_template_data:
    models.execute_kw(db, uid, password, 'product.template', 'write', 
        [[ptd['id']], 
        {
            'categ_id': ptd['categ_id'][0],
            ...
        }
    ])

You mentioned that you need to transfer data to another database, the product template is probably not present which means that you can't call the write method instead, you can call the create method.你提到你需要将数据传输到另一个数据库,产品模板可能不存在这意味着你不能调用 write 方法,你可以调用create方法。

Example:例子:

id = models.execute_kw(db, uid, password, 'product.template', 'create', [{
    'categ_id': ptd['categ_id'][0],
    ...
}])  

Edit:编辑:
You will get an invalid syntax error using:你会得到一个无效的语法错误:

[product,'categ_id': product['categ_id'][0],]

To pass values to the create method, you need to pass args to the execute_kw method as a list then pass values as a dictionary inside that list.要将值传递给 create 方法,您需要将args作为列表传递给execute_kw方法,然后在该列表中将值作为字典传递。

Edit:编辑:


values = {}
values['name'] = product['name']
values['categ_id'] = product['categ_id'][0]
...
new_lead = models_2.execute_kw(db_2, uid_db2, password_db_2, 'product.template', 'create', [values])

Edit: Use the parent category id in the new database编辑:在新数据库中使用父类别 ID

When we call the create method it will create a new record and return its ID which is probably different the one passed through the values dictionary.当我们调用 create 方法时,它将创建一个新记录并返回它的ID ,这可能与通过values字典传递的 ID 不同。

To avoid the ValidationError you can use a dictionary where the parent ID in the old database is the key and the new ID is the value then you have just to pass that value when creating a new category.为避免ValidationError ,您可以使用字典,其中旧数据库中的父 ID 是键,新 ID 是值,然后您只需在创建新类别时传递该值。

category_ids = {}
for category in db_1_categories:
        print("category..", category)
        total_count +=1
        values = {}
        # values['id'] = category['id']
        values['name'] = category['name']
        if category['parent_id']:
            values['parent_id'] = category_ids[category['parent_id'][0]]
        category_id = models_2.execute_kw(db_2, uid_db2, password_db_2, 'product.category', 'create', [values])
        category_ids[category['id']] = category_id

first of all error in your code首先是你的代码中的错误

This is your code这是你的代码

db_1_products = models_1.execute_kw(db_1, uid_db1, password_db_1, 'product.template', 'search_read', [[]], {'fields':['id','name', 'categ_id','type', 'default_code', 'list_price', 'website_url', 'inventory_availibility', 'website_description']}) db_1_products = models_1.execute_kw(db_1, uid_db1, password_db_1, 'product.template', 'search_read', [[]], {'fields':['id', 'name', 'categ_id', 'type', ' default_code', 'list_price', 'website_url', 'inventory_availibility', 'website_description']})

total_count = 0
for product in db_1_products:
    print("produt..", product)
    total_count +=1
    new_lead = models_2.execute_kw(db_2, uid_db2, password_db_2, 'product.template', 'create', [product,'categ_id': product['categ_id'][0],])

print("Total Created..", total_count) print("创建总数..", total_count)

This is updated code这是更新的代码

db_1_products = models_1.execute_kw(db_1, uid_db1, password_db_1, 'product.template', 'search_read', [[]], {'fields':['id','name', 'categ_id','type', 'default_code', 'list_price', 'website_url', 'inventory_availibility', 'website_description']}) db_1_products = models_1.execute_kw(db_1, uid_db1, password_db_1, 'product.template', 'search_read', [[]], {'fields':['id', 'name', 'categ_id', 'type', ' default_code', 'list_price', 'website_url', 'inventory_availibility', 'website_description']})

total_count = 0
for product in db_1_products:
    print("produt..", product)
    total_count +=1
    new_lead = models_2.execute_kw(db_2, uid_db2, password_db_2, 'product.template', 'create', [{product,'categ_id': product['categ_id'][0]}])

print("Total Created..", total_count) print("创建总数..", total_count)

you need to pass dictionary when creating of any model in odoo.在 odoo 中创建任何 model 时需要传递字典。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM