Skip to Content
Menu
This question has been flagged
4 Replies
2381 Views

Hi all,

i want to make the list of booked Events available on employee form in odoo 11. Therefore I query Event.registration for a given Partner ID and intend to put the corresponding Event in a one2many field on employee form. However,  the one2many field events_per_user remains empty. The result of the query on Event.registration is valid, though.

I appreciate any help on this!


class Employee(models.Model):
    _name = "hr.employee"

[...]

  events_per_employee = fields.One2many('event.event', compute='_compute_event_list')

    def _compute_event_list(self):
       #get related Partner, as there a n related Partners (Private adress, Company, ...)

       partnerID = self[0].user_id.partner_id.id
       event_list = self.env['event.registration'].search([('partner_id', '=', partnerID)])
       for event in event_list:
        if(event.event_id):
            print(event.event_id.id)
            self.events_per_employee.write({'id': event.event_id.id})
[...]
 

Avatar
Discard
Best Answer

While Juan's code will work, it can be done cleaner. The main issues are:

  • The use of _name instead of _inherit. As you are inheriting, not defining the hr.employee model, use _inherit

  • The use of a one2many where a many2many is the more appropriate datatype. Multiple employees to multiple events.

  • The use of api.one for a compute where it is unnecessary. Using a for "rec in self" loop is standard Odoo practice to allow multiple records to compute in a single function call.

class Employee(models.Model):
    _inherit = "hr.employee"
 
    events_per_employee = fields.Many2many(comodel_name='event.event', compute='_compute_event_list')
 
    @api.depends('user_id')
    def _compute_event_list(self):
        for rec in self:
            rec.events_per_employee = self.env['event.registration'].search([('partner_id', '=', rec.user_id.partner_id.id)]).mapped('event_id')


Avatar
Discard
Best Answer

class Employee(models.Model):

    _inherit = "hr.employee"

    

    events_per_employee = fields.One2many('event.event', compute='_compute_event_list')


    ## the following Definition will call always when you click on employee

    def _compute_event_list(self):

        for rec in self:

            rec.events_per_employee = self.env['event.registration'].search([('partner_id', '=', rec.user_id.partner_id.id)]).mapped('event_id')


    

    ## the following Definition will call only 'user_id' is modified or some modification on user table

    @api.depends('user_id')

    def _compute_event_list(self):

        for rec in self:

            rec.events_per_employee = self.env['event.registration'].search([('partner_id', '=', rec.user_id.partner_id.id)]).mapped('event_id')


    Note : Please decide which one do you want.

Avatar
Discard
Best Answer

Hi Matthias,

You almost got it! I think the best way to avoid this problem in the future is to read a little bit more about the ORM mechanics, especially the @api.one/@api.multi decorators (using self[0] in this case would get you in trouble in views that show multiple employees such as tree view).

The reason your code is not working is the line:

self.events_per_employee.write({'id': event.event_id.id})
because you have to assign to events_per_employee a recordset of event.event:

class Employee(models.Model):
    _name = "hr.employee"

[...]

  events_per_employee = fields.One2many('event.event', compute='_compute_event_list')

@api.one

@api.depends('user_id')

  def _compute_event_list(self):
       #get related Partner, as there a n related Partners (Private adress, Company, ...)

           partnerID = self.user_id.partner_id.id
           employee_registration_list = self.env['event.registration'].search([('partner_id', '=', partnerID)])

employee_events = [registration.event_id for registration in employee_registration_list if registration.event_id]

self.events_per_employee = employee_events
 

Avatar
Discard
Author Best Answer

Thanks to all of you, works fine!

Just to have it documented, error message below Points to missing reference to event in section 'depends' of manifest file:

ValueError: Wrong value for hr.employee.events_per_employee: event.event(4, 2)


Avatar
Discard