这些瞬态模型的ID发生了什么? [英] What's happening with these transient models' IDs?
问题描述
代码
我有以下瞬态模型:
class MoveLotsManager(models.TransientModel):
_name = 'move.lots.manager'
product_lots_available = fields.One2many(
comodel_name='move.product.lot.available',
inverse_name='manager_id',
string='Available lots',
)
class MoveProductLotAvailable(models.TransientModel):
_name = 'move.product.lot.available'
manager_id = fields.Many2one(
comodel_name='move.lots.manager',
string='Lots Manager',
)
name = fields.Char(
string='Name',
)
@api.one
@api.onchange('name')
def onchange_name(self):
# LOGGER 4
_logger.info(self.manager_id)
# LOGGER 5
_logger.info(self.manager_id.id)
如您所见,它们都是通过1:N关系连接的.我以这种方式打开瞬态模型的视图:
As you can see, they both are connected through a 1:N relationship. I open the transient models' views this way:
@api.multi
def open_move_lots_manager_wizard(self):
self.ensure_one()
wizard_id = self.env.ref(
'my_module.view_move_lots_manager_wizard').id
default_lots = [(
(0, 0, {
'name': 'My lot',
})
)]
lots_manager = self.env['move.lots.manager'].create({
'product_lots_available': default_lots,
})
# LOGGER 1
_logger.info(lots_manager)
# LOGGER 2
_logger.info(lots_manager.id)
# LOGGER 3
_logger.info(lots_manager.product_lots_available.mapped('manager_id'))
return {
'name': _('Lots manager'),
'view_type': 'form',
'view_mode': 'form',
'view_id': False,
'res_id': lots_manager.id,
'views': [(wizard_id, 'form'), ],
'res_model': 'move.lots.manager',
'type': 'ir.actions.act_window',
'target': 'new',
'flags': {
'form': {
'action_buttons': False,
},
},
}
我的目的
在模型move.product.lot.available
的onchange_name
方法中,我要访问其相关的管理器(及其字段).
In the onchange_name
method of the model move.product.lot.available
, I want to access to its related manager (and their fields).
预期的行为
想象一下我刚刚创建的move.lots.manager
具有ID 11.
Imagine the move.lots.manager
I've just created has the ID 11.
LOGGER 1: move.lots.manager(11,)
LOGGER 2: 11
LOGGER 3: move.lots.manager(11,)
LOGGER 4: move.lots.manager(11,)
LOGGER 5: 11
当前行为
LOGGER 1: move.lots.manager(11,)
LOGGER 2: 11
LOGGER 3: move.lots.manager(11,)
LOGGER 4: move.lots.manager(<openerp.models.NewId object at 0x7fd1a60cb850>,)
LOGGER 5: <openerp.models.NewId object at 0x7fd1a60cb850>
我的问题
我知道瞬态模型不是以永久方式存储的,而是根据数据库的系统参数在一段时间后将其删除.但是,当它们在数据库中时,它们确实具有ID.实际上,当我打开瞬态模型表单(激活了开发人员模式)并单击 View Metadata 选项时,我可以看到 ID: 11 ...那为什么不能从子"瞬态模型中得到它?
I know that transient models aren't stored in a permanent way, they're removed after a while, according to the system parameters of the database. But, while they're in the database, they do have an ID. And I can get it as you see, in fact, when I have the transient model form opened (with the developer mode activated) and I click on View Metadata option, I can see ID: 11... So why can't I get it from the "child" transient model?
推荐答案
看看 onchange()行4971及以下. Odoo正在缓存/内存中创建新记录,稍后将在完成onchange之后,使用提供的数据更新自己的缓存.
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.
如果您确实需要ID或其他字段,请使用<record>._origin.<field>
.
If you really need the ID or other fields use <record>._origin.<field>
.
不要在onchange方法上使用api.one
装饰器.无论如何,它们都是在单例记录上触发的.
Don't use api.one
decorator on onchange methods. They are triggered on singleton records anyway.
这篇关于这些瞬态模型的ID发生了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!