读取记录
记录数据可以通过read()方法访问,该方法获取一个id列表(由search()返回),还可以选择获取一个字段列表。默认情况下,它获取当前用户可以读取的所有字段,这往往是一个巨大的数量。
ids = models.execute_kw(db, uid, password, 'res.partner', 'search', [[['is_company', '=', True]]], {'limit': 1})
[record] = models.execute_kw(db, uid, password, 'res.partner', 'read', [ids])
# count the number of fields fetched by default
len(record) # 返回121
models.execute_kw(db, uid, password, 'res.partner', 'read', [ids], {'fields': ['name', 'country_id', 'comment']})
# 返回 [{"comment": false, "country_id": [21, "Belgium"], "id": 7, "name": "Agrolait"}]
列出记录字段
fields_get()可用于检查模型的字段,并检查哪些字段似乎感兴趣。因为它返回了大量的元信息(客户端程序也使用它),所以在打印之前应该对其进行过滤,所以对人类用户来说,最有趣的项目是字符串(字段的标签)、帮助(如果可用,则为帮助文本)和类型(了解预期值或更新记录时发送的值)。
models.execute_kw(db, uid, password, 'res.partner', 'fields_get', [], {'attributes': ['string', 'help', 'type']})
返回:
{
"ean13": {
"type": "char",
"help": "BarCode",
"string": "EAN13"
},
"property_account_position_id": {
"type": "many2one",
"help": "The fiscal position will determine taxes and accounts used for the partner.",
"string": "Fiscal Position"
},
"signup_valid": {
"type": "boolean",
"help": "",
"string": "Signup Token is Valid"
},
"date_localization": {
"type": "date",
"help": "",
"string": "Geo Localization Date"
},
"ref_company_ids": {
"type": "one2many",
"help": "",
"string": "Companies that refers to partner"
},
"sale_order_count": {
"type": "integer",
"help": "",
"string": "# of Sales Order"
},
"purchase_order_count": {
"type": "integer",
"help": "",
"string": "# of Purchase Order"
},
搜索并阅读
因为这是一个非常常见的任务,Odoo提供了一个search_read()快捷方式,顾名思义,它相当于search()后面跟着read(),但避免了执行两个请求并保留id。它的参数与search()类似,但它也可以获取字段列表(如read(),如果没有提供该列表,它将获取匹配记录的所有字段)。
models.execute_kw(db, uid, password, 'res.partner', 'search_read', [[['is_company', '=', True]]], {'fields': ['name', 'country_id', 'comment'], 'limit': 5})
返回:
[
{
"comment": false,
"country_id": [ 21, "Belgium" ],
"id": 7,
"name": "Agrolait"
},
{
"comment": false,
"country_id": [ 76, "France" ],
"id": 18,
"name": "Axelor"
},
{
"comment": false,
"country_id": [ 233, "United Kingdom" ],
"id": 12,
"name": "Bank Wealthy and sons"
},
{
"comment": false,
"country_id": [ 105, "India" ],
"id": 14,
"name": "Best Designers"
},
{
"comment": false,
"country_id": [ 76, "France" ],
"id": 17,
"name": "Camptocamp"
}
]