当我使用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:
获取数据库ID或ID列表并返回记录集,当从Odoo外部获取记录ID(例如,通过外部系统往返)或在旧API中调用方法时很有用
browse
中使用的所有记录是否在数据库中?答案 0 :(得分:2)
是的,这是正常行为。检查brow方法。此行已注释
assert all(isinstance(id, IdType) for id in ids), "Browsing invalid ids: %s" % ids
但是您有方法存在
exists() 返回一个仅包含数据库中存在的记录的新记录集。可用于检查记录(例如从外部获取)是否仍然存在: