通过XML-RPC与Odoo交互

时间:2017-10-22 21:28:53

标签: json xml-rpc odoo erp

在我的实习期间,我被告知构建一个Django应用程序,通过XML-RPC从Odoo检索数据,据我所知,xmlrpc Web服务器仅支持XML格式,但是当我按照Odoo10 {{3}的官方文档时在“搜索和阅读”部分,我觉得响应是JSON格式(这对我有好处)。我只是想知道我是否遗漏了什么。

有任何澄清吗?感谢。

1 个答案:

答案 0 :(得分:1)

不完全是JSON,而是一个Python对象(列表,字典等),可以通过dumps库的json方法转换为JSON字符串。看一下使用XMLRPC的Python控制台的行为。首先,我进行了查询(还有search_read方法)。然后我要求接收结果的类型,这是一个数组(Python对象)。然后,要将此Python对象转换为JSON字符串,我使用dumps方法。那里有你的JSON。

>>> res = models.execute_kw(
    db,
    uid,
    password,
    'res.partner',
    'search_read',
    [[
        ['is_company', '=', True],
        ['customer', '=', True]
    ]],
    {
        'fields': ['name', 'country_id', 'comment'],
        'limit': 5
    }
)

>>> res
[
    {
        'comment': False,
        'country_id': [69, 'Spain'],
        'id': 1665,
        'name': 'Customer A'
    }, {
        'comment': False,
        'country_id': [69, 'Spain'],
        'id': 5799,
        'name': 'Customer B'
    }, {
        'comment': False,
        'country_id': [69, 'Spain'],
        'id': 1946,
        'name': 'Customer C'
    }, {
        'comment': False,
        'country_id': [69, 'Spain'],
        'id': 1367,
        'name': 'Customer D'
    }, {
        'comment': False,
        'country_id': [69, 'Spain'],
        'id': 2066,
        'name': 'Customer E'
    }
]

>>> type(res)
<type 'list'>

>>> import json
>>> json.dumps(res)

'[{"comment": false, "country_id": [69, "Spain"], "id": 1665, "name": "CUSTOMER A"}, {"comment": false, "country_id": [69, "Spain"], "id": 5799, "name": "CUSTOMER B"}, {"comment": false, "country_id": [69, "Spain"], "id": 1946, "name": "CUSTOMER C"}, {"comment": false, "country_id": [69, "Spain"], "id": 1367, "name": "CUSTOMER D"}, {"comment": false, "country_id": [69, "Spain"], "id": 2066, "name": "CUSTOMER E"}]'

>>> your_json = json.dumps(res)  # This variable is ready to be sent as JSON

由于@ChesuCR,我已经更新了我的答案以澄清它。