You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Odoo10中XML-RPC查询res.partner.title无法返回正确ID求助

解决Odoo10 XML-RPC查询res.partner.title无法返回正确ID的问题

看起来你遇到的问题和Odoo10对多语言字段的处理变化直接相关,结合你的描述,我来拆解原因并给出具体解决方案:

问题根源

res.partner.title模型的shortcut字段是可翻译字段(Odoo源码中标记为translatable=True)。在Odoo9中,查询可翻译字段时可能直接匹配数据库表存储的原始值,但Odoo10优化了多语言处理逻辑:当上下文指定lang: 'es_ES'时,查询条件会自动匹配该语言下的翻译值,而非数据库表中存储的原始shortcut值。

如果你的数据库里ID=4的记录原始shortcut是英文的Mr.,但西班牙文翻译对应的shortcut不是这个值,那么用('shortcut','=','Mr.')在es_ES上下文下自然查不到正确记录,你看到的1或0大概率是返回的ID列表长度,而非实际记录ID。

解决方案

1. 临时切换上下文语言查询原始值

在调用search方法时,临时将上下文语言改为原始值对应的语言(比如英文en_US),或者直接移除lang参数,这样查询会匹配数据库表中存储的原始shortcut值:

# 示例Python XML-RPC代码
import xmlrpc.client

common = xmlrpc.client.ServerProxy('http://your_odoo_url/xmlrpc/2/common')
models = xmlrpc.client.ServerProxy('http://your_odoo_url/xmlrpc/2/object')
db_name = "your_db"
username = "your_user"
password = "your_pass"

uid = common.authenticate(db_name, username, password, {})

# 修改上下文为英文,或者直接传空字典{}
context = {'lang': 'en_US'}
partner_title_ids = models.execute_kw(
    db_name, uid, password,
    'res.partner.title', 'search',
    [[('shortcut', '=', 'Mr.')]],
    {'context': context}
)
# 正确情况下会返回[4],取第一个元素就是目标ID
target_id = partner_title_ids[0] if partner_title_ids else None

2. 直接查询原始未翻译字段

Odoo支持通过字段名加_original后缀来绕过翻译,直接查询数据库表中存储的原始值,即使上下文指定了其他语言:

partner_title_ids = models.execute_kw(
    db_name, uid, password,
    'res.partner.title', 'search',
    [[('shortcut_original', '=', 'Mr.')]],
    {'context': {'lang': 'es_ES'}}
)

3. 确认目标记录的西班牙文翻译值

如果必须在西班牙文上下文下查询,先确认ID=4的记录在西班牙文下的shortcut翻译值是什么,再用该值作为查询条件:

# 获取ID=4的记录在西班牙文下的shortcut值
partner_title_data = models.execute_kw(
    db_name, uid, password,
    'res.partner.title', 'read',
    [4],
    {'fields': ['shortcut'], 'context': {'lang': 'es_ES'}}
)
# 打印结果查看翻译后的值,比如可能是'Sr.'
print(partner_title_data[0]['shortcut'])

# 用翻译后的值查询
partner_title_ids = models.execute_kw(
    db_name, uid, password,
    'res.partner.title', 'search',
    [[('shortcut', '=', 'Sr.')]],  # 替换为实际翻译值
    {'context': {'lang': 'es_ES'}}
)

额外排查点

  • 检查目标记录(ID=4)的active字段是否为True:Odoo默认会过滤归档(active=False)的记录,如果该记录被归档,需要在查询条件中添加('active', '=', True)或者('active', 'in', [True, False])。
  • 确认代码逻辑:确保你没有误将search方法返回的ID列表长度当成了ID本身,search返回的是匹配记录的ID数组,需要取数组中的元素(比如partner_title_ids[0])才能得到具体ID。

内容的提问来源于stack exchange,提问作者cucuru

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 06:56:00