Search code examples
odoo-8odooodoo-10odoo-9erp

Odoo : write method is only invoked when updating a model


Inside a model which I have created, I implemented the write method to update another model fields , so I want to invoke this operation on every record creation but apparently Odoo invokes the method only when updating the record.

Here it is an excerpt from the model:

class formulaire_evaluation(models.Model):
     _name = 'pncevaluation.fe'
     _description = u"Formulaire d\'évaluation"
     name = fields.Char(u"Intitulé du formulaire")
     contributeur = fields.Many2one('pncevaluation.contributeur',string="Contributeur")
     date = fields.Date(u"Date")


     @api.multi
     def write(self, vals):
        rec = super(formulaire_evaluation, self).write(vals)
        #my_custom_code
        return rec

Solution

  • At the time of record creation, Odoo calls ORM create() method. So in your case we need to override create() method.

    For example:

    @api.model
    def create(self, vals):
        res = super(formulaire_evaluation, self).create(vals)
        #### your logic to update another model
        return res
    

    For more details Odoo's ORM methods

    NOTE:

    If you want to do this operation only at record creation then go with create() method and remove write() method. Otherwise go with both methods create() and write().