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?
[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