This question has been flagged
1 Reply
3754 Views

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

Avatar
Discard
Best Answer

You can do this from the onchange of the project and return the domain.

Ex:

api.onchange('project_id')
def onchange_project(self):
    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)]}}
Sudhir Arya
ERP Harbor Consulting Services
Skype:sudhir@erpharbor.com
Website: http://www.erpharbor.com
Avatar
Discard
Author

Thank you. I tried but got error. What can be wrong with that approach?

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:

tag_ids += [task.tag_ids.ids for task in self.project_id.task_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 then I get error:

> res = self._obj.execute(query, params)

>

> TypeError: not all arguments converted during string formatting

on the console I see the error

> INFO DB odoo.sql_db: bad query: SELECT "project_tags".id FROM "project_tags" WHERE (("project_tags"."id" in (%s,%s,%s,%s,%s,%s,%s,%s)) AND (("project_tags"."id" not in (%s)) OR "project_tags"."id" IS NULL)) ORDER BY "project_tags"."id" limit 8

2018-08-03 10:40:20,476 12978 ERROR DB odoo.http: Exception during JSON request handling.

What did I do wrong in the code?

See my updated code in answer. It should work now.

Author

Thank You very much for the proposed solution! I would not come to that in near time! :) But after applying it I can see on the "Tags" field (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 really take tags from just this project's tasks. Tags from other projetcs tasks appear too which is unwanted. How this can be that all tags are included not just those from tasks of the current project?

It will only show all the tags which are used in the tasks of selected project.

Author

It is really strange. I do the following: 1) Open "Projects". 2) Click "Create" 3) Create a project with a name "Really new project". 4) I open project "Really new project" tasks window. This window is empty as we didn't create any tasks yet. 5) I click "Create" and a form to create new task appears. 6) I enter task title "This is really new task in a really new project" . 7) In the field "Tags" I click with mouse and drop-down appears with text "No results to show" and I enter "1234657890" (this tag name is never used anywhere so it's completely new tag). I click "Create 1234657890" for this tag to be created and it appears in "Tags" field. So far so good. 8) I press "Save" on the task so that the new record is saved. Task is saved and on the form appears a button "Edit". 9) without reloading a page I click "Edit" and click on the "Tags" for drop-down to appear. Drop-down appears and it contains text "No results to show" (this is good because there are no more tags in that project to be selected because we have not created any other tags). I type "qqq" in "Tags" and create this tag, then on the task form I press "Save" to save that task. Then I navigate to any other project (let's say I navigate to **Another project**) and open one of project's tasks just to see it. And then I navigate to the project "Really new project" and press "Edit" and click "Tags" and I see all the tags appear from other project's. Why?

What I have noticed. If I go to any project (project1) and open a task of it then navigate to another project (project2) and click "Create" to create new task for the project2 then on "Tags" there will be only tags from project1 tasks (as we want) it seems that domain works. BUT if I go to any project (project1) and open a task of it then navigate to another project (project2) and click "Edit" to edit a task (of the project2) then on "Tags" there will be ALL TAGS (as if domain was ineffective). How is that possible?

I have noticed (from looking at the console output) that method onchange is called only when I click "Create" on the task. So I have troubles when I want to edit task because onchange is not called and all tags show up :(.

Is there anything else I could add besides onchange to make domain apply all the time.