pythonodooodoo-18

How to compute on load?


I have a model like this:

# Calculated Fields
request = fields.Many2one('job.request', string='Request', required=True)    
e_name = fields.Char('Nama Asset', store=False, compute="_compute_initial")

@api.depends('request')
def _compute_initial(self):
    for record in self:
        r = record.request
        record.e_name = r.equipment_id.name

And I need to assign a value to e_name, taken from its parent, for display purposes only, as equipment from "job.request". The problem is that _compute_initial doesn't get executed, even though I've put the e_name field on the form, the value is empty.

So, how to execute _compute_initial to fill in the e_name field?


Solution

  • as mentioned above in the comments, for new records (and dynamic changes on the web!) you want to use onchange; always assume someone will create a record in code somewhere somehow so also make sure you keep your depends.

    request = fields.Many2one('job.request', string='Request', required=True)    
    e_name = fields.Char('Nama Asset', store=False, compute="_compute_initial")
    
    @api.onchange('request')
    @api.depends('request')
    def _compute_initial(self):
        for record in self:
            r = record.request
            record.e_name = r.equipment_id.name