为了账号安全,请及时绑定邮箱和手机立即绑定

为什么“浏览”方法使用不存在的 id 返回错误的记录集?

为什么“浏览”方法使用不存在的 id 返回错误的记录集?

繁华开满天机 2021-09-25 21:59:06
当我使用该search方法并创建一个查找不存在的 id 的记录集时,结果是预期的空记录集:>>> self.env['account.invoice'].search([('id', 'in', [23232323123123123, ])])account.invoice()但是如果我对browse方法做同样的事情,结果是一个具有该 id 的记录集,但实际上该记录不存在:>>> o = self.env['account.invoice'].browse([23232323123123123])>>> oaccount.invoice(23232323123123123,)>>> o.id23232323123123123>>> o.numberTraceback (most recent call last):File "/path/to/odoo/odoo/fields.py", line 937, in __get__    value = record.env.cache.get(record, self)File "/path/to/odoo/odoo/api.py", line 960, in get    value = self._data[field][record.id][key]KeyError: (<odoo.sql_db.Cursor object at 0x7f4d2985e9e8>, 1)During handling of the above exception, another exception occurred:Traceback (most recent call last):File "/usr/lib/python3.5/code.py", line 91, in runcode    exec(code, self.locals)File "<console>", line 1, in <module>File "/path/to/odoo/odoo/fields.py", line 937, in __get__    value = record.env.cache.get(record, self)File "/path/to/odoo/odoo/api.py", line 961, in get    return value.get() if isinstance(value, SpecialValue) else valueFile "/path/to/odoo/odoo/api.py", line 993, in getter    raise exceptionFile "/usr/lib/python3.5/code.py", line 91, in runcode    exec(code, self.locals)File "<console>", line 1, in <module>File "/path/to/odoo/odoo/fields.py", line 937, in __get__    value = record.env.cache.get(record, self)File "/path/to/odoo/odoo/api.py", line 961, in get    return value.get() if isinstance(value, SpecialValue) else valueFile "/path/to/odoo/odoo/api.py", line 993, in getter    raise exceptionFile "/usr/lib/python3.5/code.py", line 91, in runcode    exec(code, self.locals)这是文档中的解释:获取数据库 ID 或 ID 列表并返回记录集,当从 Odoo 外部获取记录 ID(例如通过外部系统往返)或调用旧 API 中的方法时很有用这是正常行为吗?我觉得这种方法没用我是否必须browse事先检查数据库中是否存在a中使用的所有记录?
查看完整描述

1 回答

?
慕婉清6462132

TA贡献1804条经验 获得超2个赞

是的,这是正常行为。检查眉毛方法。此行已注释

assert all(isinstance(id, IdType) for id in ids), "Browsing invalid ids: %s" % ids

但是你有方法存在

exists() 返回一个新的记录集,只包含数据库中存在的记录。可用于检查记录(例如从外部获得)是否仍然存在:


查看完整回答
反对 回复 2021-09-25
  • 1 回答
  • 0 关注
  • 232 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信