Nabil Taleb
Nabil Taleb

Reputation: 137

Expected singleton: line.employee.mission(57, 58, 59) !! (Odoo 11)

I have a problem with my code, I want to check Is an employee already caught in the chosen dates! but the employee is in a field One2Many, here is my code:

My Error :

Expected singleton: line.employee.mission(57, 58, 59)

In Py file :

    class HrMission(models.Model):
        _name = 'hr.employee.mission'
        _description = 'Mission object'
        _inherit = 'mail.thread'

        line_mission = fields.One2many('line.employee.mission','line_mission', string='Employee', help="Employee sent on mission", copy=True,auto_join=True)

    @api.multi
    @api.constrains('mission_start_date', 'mission_end_date')
    def _check_date(self):
        for mission in self:
            domain = [
                ('mission_start_date', '<=', mission.mission_end_date),
                ('mission_end_date', '>=', mission.mission_start_date),
                ('line_mission', '=', mission.line_mission.id),
                ('id', '!=', mission.id),
            ]
            nmissions = self.search_count(domain)
            if nmissions:
                raise exceptions.ValidationError('Vous ne pouvez pas avoir 2 missions qui se chevauchent le même jour!')

    class LineEmployee(models.Model):
        _name = 'line.employee.mission'
        _description = 'Lignes des employés pour les mission'

        line_mission = fields.Many2one('hr.employee.mission', string='Line mission Reference', required=True, ondelete='cascade', index=True, copy=False)
        employee_id = fields.Many2one('hr.employee', string='Employee', help="Employee sent on mission",
                                      required=True)
        job_id = fields.Many2one(related='employee_id.job_id', string='Fonction', help="Fonction employee", required=True)
        department_id = fields.Many2one(related='employee_id.department_id', string='Département',required=True)

In XML file :

<page name="Employés" string="Employés">
                                    <field name='line_mission'>
                                            <tree string='Employés' editable="top">
                                                <field name='employee_id'/>
                                                <field name='job_id'/>
                                                <field name='department_id'/>
                                            </tree>
                                    </field>
                            </page>

Upvotes: 0

Views: 130

Answers (1)

Adrian Merrall
Adrian Merrall

Reputation: 2499

You haven't posted the full error message but looking at your code I presume this is triggering the error:

 ('line_mission', '=', mission.line_mission.id),

You have defined line_mission as a One2many so anytime the One2many record set has more than one entry, accessing ".id" will give you this singleton error.

I presume you need to know if there are any other hr.employee.mission records with a date overlapping and where the employee is on one the lines.

You could use a single SQL query with joins but if I were doing this in a search I would probably do two searches to prevent Odoo building a potentially big list of the parent record.

# find any other missions that overlap our dates.
overlapping_missions = self.search([
    ('mission_start_date', '<=', mission.mission_end_date),
    ('mission_end_date', '>=', mission.mission_start_date),
    ('id', '!=', mission.id)])
if overlapping_missions:
    # Are there any lines for these overlapping missions that have any 
    # of the same employees as we have for this mission (note list comprehension).
    duplicates = self.env['line.employee.mission'].search_count([
        ('line_mission', 'in', overlapping_parents.ids),
        ('employee_id', 'in', [l.employee_id.id for l in mission.line_mission])
    ])
    if duplicates:
       raise ValidationError(...)

I am at home so haven't tested this but it should be roughly right.

Upvotes: 1

Related Questions