我的代码有问题,我想检查所选日期是否已经有一名雇员!但是员工在One2Many
字段中,这是我的代码:
我的错误:
预期的单例:line.employee.mission(57,58,59)
在Py文件中:
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)
在XML文件中:
<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>
答案 0 :(得分:1)
您尚未发布完整的错误消息,但查看您的代码,我想这会触发错误:
('line_mission', '=', mission.line_mission.id),
您已将line_mission定义为One2many,因此只要One2many记录集具有多个条目,则访问“ .id”将给您此单例错误。
我想您需要知道是否还有其他hr.employee.mission记录,这些记录的日期重叠并且员工在一行上。
您可以对联接使用单个SQL查询,但是如果我在搜索中执行此操作,则可能会执行两次搜索,以防止Odoo建立父记录的潜在大列表。
# 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(...)
我在家里,所以还没有测试,但这应该是正确的。