Search code examples
pythontagsmany-to-manyodoo-10

Make project task tags scoped to the project's tags


There is a model

class project_tag(models.Model):

    _name = 'project_tags.project_tag'
    _description = 'project_tag'

    name = fields.Char(string='Name', required=True, size=64)

Then there is project model

class project(models.Model):
    _inherit = 'project.project'

    project_tag_ids = fields.Many2many(
        'project_tags.project_tag',
        'project_project_tag_ids_rel',
        'project_id',
        'project_tag_id',
        string='Tags')

And finally a model

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

    tag_ids = fields.Many2many(
        'project_tags.project_tag',
        'project_project_tag_ids_rel',
        'project_id',
        'project_tag_id',
        string='Tags',
        domain="[('project_id', 'in', project_id)]") .

The view for project task (model ProjectTask) contains field

<field name="tag_ids" widget="many2many_tags" options="{'no_create_edit': True}"/>

The user would like to see in tag_ids only those tags, that are configured for the project this task belongs to. For the given task's project there are no tags configured, so tag_ids should be empty but it's not :(.

Project tags (in this example it's empty). enter image description here

I am hoping that domain would help. Unfortunately when tag_ids field definition is with domain

tag_ids = fields.Many2many(
        'project_tags.project_tag',
        'project_project_tag_ids_rel',
        'project_id',
        'project_tag_id',
        string='Tags',
        domain="[('project_id', 'in', project_id)]") .

the user sees an error

ValueError: Invalid field u'project_id' in leaf "<osv.ExtendedLeaf: (u'project_id', u'=', 10) on project_tags_project_tag (ctx: )>"

When without a domain it shows all the tags

tag_ids = fields.Many2many(
    'project_tags.project_tag',
    'project_project_tag_ids_rel',
    'project_id',
    'project_tag_id',
    string='Tags') 

enter image description here

User expects to see only those tags that are assigned for the project.

The project manager can on the project window add tags to the project. The user that edits or creates a task should be allowed to choose only those tags that are added for the project.

How can I modify code so that tag_ids shows only those tags that are for the project_id the task belongs to?


Solution

  • I ended up installing module Project tags. Then I configured project task to show only those tags to choose from, whose name is in the list of project tags.

    :)