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

标签 python python-3.x python-2.7 orm odoo

当我使用 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/

相关文章:

python - 在python中找到特定字符串后如何打印所有行?

python - 在不需要 update_index 的情况下排除 Haystack 搜索中的对象

python - 如何在 Python 中格式化输出?

python - 用于实时 appengine 项目的基于浏览器的交互式控制台的 url 是什么?

python - python中的平等和继承

Python-数据帧 : Multiply multiple columns by another column and save in new columns

Python 模块适用于 2.7,但不适用于 3.5

android - 从现有实例调用方法

python - 在 Python 中打印精确的错误消息

python - 如何使用 python 保持范围?