1
votes

Project's task can have many tags. Now when the user creates a task he can choose from all tags. But he wants to be able to choose tags only from those tags that are used for that project.

What domain rule should I have on the field "tag" (on the form) to let user when creating new project task to see (and eventually to choose) only those tags that are used in the project the user creates task for?

What I tried and didn't work.

I have the following code

class InheritedProjectTask(models.Model): """docstring""" _inherit = 'project.task'

@api.onchange('project_id')
def onchange_project(self):
    """docstring"""
    tag_ids = []
    if self.project_id:
        for task in self.project_id.task_ids:
            tag_ids += task.tag_ids.ids
    return {'domain': {'tag_ids': [('id', 'in', tag_ids)]}}

With this code installed. I open a Project and click "Create" to open a Task creation form (form for new "project.task" model's object). On that form I click on drop-down "Tags" to list possible "Tags" for the project ("possible tags for the project" are all tags that are used on any of the task of the project the task is created for). And I see all the tags. This is not wanted.

Update: I can see on the "Tags" drop-down (in debug mode when I hover over "Tags" drop down) that effective Domain is id, in,4,5,6,7,8,9,10,11,12,13,14,18,19,20,22,23,24,25,26,27,28,5,4,5,35 . And that is the problem: it doesn't take tags from just this project's tasks. Tags from other projects tasks appear too which is unwanted.

How this can be that all tags are included not just those from tasks of the current project?

1
Seems like projects themselves don't have a 'tag_ids' attribute. Do you mean that users should be able to select only tags that are used in other tasks of this project ? In this case, for the first task of a project, they wouldn't be able to select anything - Harlan
@Harlan yes for the first project the list will be empty so he will not have to select from, in that case the user can "Create" a tag for this task. Yes, project doesn't have tags so project's tags are "all tags that are assigned to any of the task of that project". - Developer Marius Žilėnas
Oh ok I didn't think about the "Create" possibility. So what comes to my mind is a domain like [('id', 'in', project_id.task_ids.tag_ids)] but I can't ever remember if you're allowed to do 'ids.something'. If it doesn't work, you could inherit from project.project to add a tag_ids Many2many field that is computed to something like [task.tag_ids for task in self.task_ids] (but you need a proper function that returns a flat list). Then the domain would be [('id', 'in', project_id.tag_ids)}. Tell me if I'm not clear enough. - Harlan
@Harlan thank you I tried that approach but couldn't make that work. Could you please see the code I put in the question update. - Developer Marius Žilėnas

1 Answers

1
votes

The tag model has a name field, so you can add your domain rule based on it.for example name starts with or name ends with etc.

But you can add a new field (project_id) in project.tags to specify to which project the tag belong to, then in the task form you can use for example [('project_id', '=', project_id)].