为什么 "browse" 方法 return 使用不存在的 id 的错误记录集?

Why does the "browse" method return a wrong recordset using a non-existing id?

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