>> self.env['account.invoice'].search([('id', 'in', -6ren">
gpt4 book ai didi

python - 为什么 "browse"方法使用不存在的 ID 返回错误的记录集?

转载 作者:行者123 更新时间:2023-12-01 01:19:52 24 4
gpt4 key购买 nike

当我使用 search 方法并创建一个记录集来查找不存在的 id 时,结果是预期的空记录集:

>>> self.env['account.invoice'].search([('id', 'in', [23232323123123123, ])])
account.invoice()

但是如果我对 browse 方法执行相同操作,结果是具有该 id 的记录集,但实际上该记录不存在:

>>> o = self.env['account.invoice'].browse([23232323123123123])
>>> o
account.invoice(23232323123123123,)
>>> o.id
23232323123123123
>>> o.number
Traceback (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 value
File "/path/to/odoo/odoo/api.py", line 993, in getter
raise exception
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 value
File "/path/to/odoo/odoo/api.py", line 993, in getter
raise exception
File "/usr/lib/python3.5/code.py", line 91, in runcode
exec(code, self.locals)
File "<console>", line 1, in <module>
File "/opt/odoo/odoo_11/src/linked/l10n_es_account_invoice_sequence/models/account_invoice.py", line 64, in unlink
self.filtered(lambda x: x.journal_id.invoice_sequence_id).write(
File "/path/to/odoo/odoo/models.py", line 4540, in filtered
return self.browse([rec.id for rec in self if func(rec)])
File "/path/to/odoo/odoo/models.py", line 4540, in <listcomp>
return self.browse([rec.id for rec in self if func(rec)])
File "/opt/odoo/odoo_11/src/linked/l10n_es_account_invoice_sequence/models/account_invoice.py", line 64, in <lambda>
self.filtered(lambda x: x.journal_id.invoice_sequence_id).write(
File "/path/to/odoo/odoo/fields.py", line 944, 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 value
File "/path/to/odoo/odoo/api.py", line 993, in getter
raise exception
File "/path/to/odoo/odoo/models.py", line 2601, in read
values[name] = field.convert_to_read(record[name], record, use_name_get)
File "/path/to/odoo/odoo/models.py", line 4758, in __getitem__
return self._fields[key].__get__(self, type(self))
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 value
File "/path/to/odoo/odoo/api.py", line 993, in getter
raise exception
odoo.exceptions.MissingError: ('Record does not exist or has been deleted.', None)

这是the explanation in the documentation :

Takes a database id or a list of ids and returns a recordset, useful when record ids are obtained from outside Odoo (e.g. round-trip through external system) or when calling methods in the old API

  • 这是正常行为吗?我觉得这个方法有点没用
  • 我是否必须事先检查数据库中是否存在浏览中使用的所有记录?

最佳答案

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

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

但是你有这个方法存在

exists() Returns a new recordset containing only the records which exist in the database. Can be used to check whether a record (e.g. obtained externally) still exists:

关于python - 为什么 "browse"方法使用不存在的 ID 返回错误的记录集?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53932302/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com