Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
718 views
in Technique[技术] by (71.8m points)

python - What's happening with these transient models' IDs?

THE CODE

I have the following transient models:

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)

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,
            },
        },
    }

MY PURPOSE

In the onchange_name method of the model move.product.lot.available, I want to access to its related manager (and their fields).

THE EXPECTED BEHAVIOUR

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

THE CURRENT BEHAVIOUR

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>

MY QUESTION

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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>.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...