Skip to Content
Menu
This question has been flagged
1 Reply
2325 Views

I want to create a task and assign it to a new project


for project in data: 
    project_tasks = project['tasks'] 
    for task in project_tasks: 
        project_id = get_or_create_project(project['name']) 
        employee_id = get_or_create_employee(task['assigned_to']) 
        new_task = request.env['project.task'].sudo().with_context(check_move_validity=False).create({ 
        'name': task['name'], 
        'description': task['desc'],        
}) 
        new_task.update({'user_id': [( 4, employee_id, 0  )]}) 
        new_task.update({'project_id': [( 4, project_id, 0 )]})


def get_or_create_project(project_name):
p_exists = request.env['project.project'].search([('display_name', '=', project_name)])
if p_exists:
project_id = p_exists.id
else:
new_project = request.env['project.project'].sudo().with_context(check_move_validity=False).create({
'name': project_name,
})
project_id = new_project.id
return project_id

def get_or_create_employee(employee_name):
e_exists = request.env['hr.employee'].search([('name', '=', employee_name)])
if e_exists:
employee_id = e_exists.id
else:
new_employee = request.env['hr.employee'].sudo().with_context(check_move_validity=False).create({
'name': employee_name,
})
employee_id = new_employee.id
return employee_id


Thank you










Avatar
Discard
Best Answer

Hi,

You can pass the project_id value in the dictionary to create method of the tasks.

new_task = self.env['project.task'].create({'project_id: project_id, 'name': 'Test'})


Thanks

Avatar
Discard
Author

I tried to do this before, but:

the project is not assigned and not created (I get: Non-stored field project.project.display_name cannot be searched. )

the employee is created but not assigned to the task.

Author

ok, I got rid of the non-stored error by extending the project.project model:

class ProjectExt(models.Model):

_inherit = 'project.project'

display_name = fields.Char(store=True)

The project and the task are created, but inside the task the project is not assigned.

Also the user is not assigned

Related Posts Replies Views Activity
1
Nov 24
1482
1
Nov 24
1188
2
Sep 24
1047
1
Aug 24
2451
3
Aug 24
2684