odooodoo-11

Odoo mail.activity creation


I'm trying to create a custom activity using 'mail.activity' model, with custom values, and show me this error:

File "/usr/lib/python3/dist-packages/odoo/addons/mail/models/mail_activity.py", line 108, in 
_compute_res_name
activity.res_name = self.env[activity.res_model].browse(activity.res_id).name_get()[0][1]
IndexError: list index out of range

I think is because in the core mail.activity model, the access to the 0 and 1 position in the array crash my program, but I dont know why.

Here is my code for the activity creation:

@api.model
def create(self, vals)
    user_id = self.user_id
    date_deadline = datetime.now() + timedelta(days=7)

    data = {
        'res_id': self.id,
        'res_model_id': self.env['ir.model'].search([('model', '=', 'hr.applicant')]).id,
        'user_id': user_id.id,
        'summary': 'foo bar',
        'activity_type_id': self.env.ref('custom.activity_applicant').id,
        'date_deadline': date_deadline
        }
    self.env['mail.activity'].create(data)
    return super().create(vals)

There is something in the field res_id that I don't know about or something else.


Solution

  • I already figured out what was wrong, I was calling self.id before even creating the object, and id is a null value then. I change the object creation before this part of the code and it works fine.

    @api.model
    def create(self, vals)
        new = super().create(vals)
        user_id = new.user_id
        date_deadline = datetime.now() + timedelta(days=7)
    
        data = {
            'res_id': new.id,
            'res_model_id': self.env['ir.model'].search([('model', '=', 'hr.applicant')]).id,
            'user_id': user_id.id,
            'summary': 'foo bar',
            'activity_type_id': self.env.ref('custom.activity_applicant').id,
            'date_deadline': date_deadline
            }
        self.env['mail.activity'].create(data)
        return new