如何从 on_change 方法中获取整数形式的 id?

How to get id as integer from on_change method?

我想 select 使用 sql 查询的不同值,为此我需要 id 作为整数。

我的代码是:

@api.onchange('qty')
def _on_change_name(self):
    logging.warning(self.id)

打印出来的id是<openerp.models.NewId object at 0x7fc37d880490>

如何从这个 id 中获取整数?

当您在具有计算字段的模型中或在 onchange 方法中创建新记录时,记录集的记录将仅在内存中。那时记录的 id 将是 openerp.models.NewId

类型的虚拟 id

因此,如果您需要在代码中使用记录 ID(例如,对于 sql 查询),您应该检查它是否可用:

if isinstance(current_record.id, models.NewId):
    # do your stuff

更新。 Zoellner 有更好的答案

Take a look into onchange() line 4971 and following. Odoo is creating a new record in cache/memory and will later, after the onchange is done, update the own cache with the provided data.

If you really need the ID or other fields use <record>._origin.<field>.

Note: Don't use api.one decorator on onchange methods. They are triggered on singleton records anyway.