Python 为什么";浏览「;方法是否使用不存在的id返回错误的记录集?

Python 为什么";浏览「;方法是否使用不存在的id返回错误的记录集?,python,python-3.x,python-2.7,orm,odoo,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'].brows

当我使用
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)
o=self.env['account.invoice'].浏览([232323123]) >>>o 账户.发票(2323123,) >>>o.id 23232323123123123 >>>o.编号 回溯(最近一次呼叫最后一次): 文件“/path/to/odoo/odoo/fields.py”,第937行,在__ value=record.env.cache.get(record,self) get中第960行的文件“/path/to/odoo/odoo/api.py” value=self.\u数据[field][record.id][key] KeyError:(,1) 在处理上述异常期间,发生了另一个异常: 回溯(最近一次呼叫最后一次): 文件“/usr/lib/python3.5/code.py”,第91行,运行代码 exec(代码,self.locals) 文件“”,第1行,在 文件“/path/to/odoo/odoo/fields.py”,第937行,在__ value=record.env.cache.get(record,self) get中第961行的文件“/path/to/odoo/odoo/api.py” 如果isinstance(value,SpecialValue)为else值,则返回value.get() 文件“/path/to/odoo/odoo/api.py”,第993行,在getter中 引发异常 文件“/usr/lib/python3.5/code.py”,第91行,运行代码 exec(代码,self.locals) 文件“”,第1行,在 文件“/path/to/odoo/odoo/fields.py”,第937行,在__ value=record.env.cache.get(record,self) get中第961行的文件“/path/to/odoo/odoo/api.py” 如果isinstance(value,SpecialValue)为else值,则返回value.get() 文件“/path/to/odoo/odoo/api.py”,第993行,在getter中 引发异常 文件“/usr/lib/python3.5/code.py”,第91行,运行代码 exec(代码,self.locals) 文件“”,第1行,在 文件“/opt/odoo/odoo\u 11/src/linked/l10n\u es\u account\u invoice\u sequence/models/account\u invoice.py”,第64行,取消链接 自我过滤(lambda x:x.journal\u id.invoice\u sequence\u id)。写入( 文件“/path/to/odoo/odoo/models.py”,第4540行,已过滤 返回self.browse([self if func(rec)]中rec的rec.id) 文件“/path/to/odoo/odoo/models.py”,第4540行,在 返回self.browse([self if func(rec)]中rec的rec.id) 文件“/opt/odoo/odoo\u 11/src/linked/l10n\u es\u account\u invoice\u sequence/models/account\u invoice.py”,第64行,在 自我过滤(lambda x:x.journal\u id.invoice\u sequence\u id)。写入( 文件“/path/to/odoo/odoo/fields.py”,第944行,在__ value=record.env.cache.get(record,self) get中第961行的文件“/path/to/odoo/odoo/api.py” 如果isinstance(value,SpecialValue)为else值,则返回value.get() 文件“/path/to/odoo/odoo/api.py”,第993行,在getter中 引发异常 文件“/path/to/odoo/odoo/models.py”,第2601行,已读 值[名称]=字段。将\u转换为\u读取(记录[名称],记录,使用\u名称\u获取) 文件“/path/to/odoo/odoo/models.py”,第4758行,在__ 返回self.\u字段[键]。\u获取(self,键入(self)) 文件“/path/to/odoo/odoo/fields.py”,第937行,在__ value=record.env.cache.get(record,self) get中第961行的文件“/path/to/odoo/odoo/api.py” 如果isinstance(value,SpecialValue)为else值,则返回value.get() 文件“/path/to/odoo/odoo/api.py”,第993行,在getter中 引发异常 odoo.exceptions.MissingError:(“记录不存在或已被删除”,无) 这是:

获取数据库id或id列表并返回一个记录集,当从外部Odoo获取记录id(例如,通过外部系统的往返)或在旧API中调用方法时非常有用

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

是的,这是正常的行为。检查browe方法。这行有注释

assert all(isinstance(id, IdType) for id in ids), "Browsing invalid ids: %s" % ids
但是你有方法存在

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


谢谢你,贾维尔。无论如何,在我看来,这行应该不加注释。