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

Hi Odoo developers,

I would want to show a many2many field from a module in its tree view but splitted in columns. Here is my example.

I have a TodoTask model with a field named tag_ids (a task may have multiple tags).

class TodoTask(models.Model):
_inherit = 'todo.task'
# Relational fields
stage_id = fields.Many2one('todo.task.stage', 'Stage')
tag_ids = fields.Many2many(
'todo.task.tag', # related= (model name)
'todo_task_tag_rel', # relation= (table name)
'task_id', # column1= ("this" field)
'tag_id', # column2= ("other" field)
string='Tags',
# Relational field attributes:
auto_join=False,
context="{}",
domain="[]",
ondelete='cascade',
)

The Tag model:

class Tag(models.Model):
_name = 'todo.task.tag'
name = fields.Char('Name', size=40, translate=True)
# Many2many inverse relation
task_ids = fields.Many2many('todo.task', string='Tasks')
# Hierarchic relations:
_parent_store = True
_parent_name = 'parent_id' # the default
parent_id = fields.Many2one(
'todo.task.tag', 'Parent Tag', ondelete='restrict')
parent_left = fields.Integer('Parent Left', index=True)
parent_right = fields.Integer('Parent Right', index=True)
child_ids = fields.One2many('todo.task.tag', 'parent_id', 'Child Tags')

The tree view associated to TodoTask model:

<record id="todo_app.view_tree_todo_task" model="ir.ui.view">
<field name="name">To-do Task Tree</field>
<field name="model">todo.task</field>
<field name="arch" type="xml">
<tree colors="gray:is_done==True"
font="italic:stage_state!='open'"
delete="false">
<field name="is_done" invisible="True"/>
<field name="stage_state" invisible="True"/>
<field name="name"/>
<field name="tag_ids"/>
</tree>
</field>
</record>

The most important part of the above view is field name="tag_ids".
tag_ids is a Many2Many field in TodoTask model (todo.task) related to Tag model (todo.task.tag).

The problem is that this tree view shows only one column for tag_ids (see column Tags in the next image).


 http://i.imgur.com/G44hQh4.png

I would like to show the Many2Many field (tag_ids) from TodoTask model in more than one column in the tree view associated to TodoTask model: one column for tag name, another column for another field from Tag model, ...

nameCourse | start_date Course | tag name | tag parent name |
------------------------------------------------------------------------------------
course 1         2015-5-15                 tag1          tag2
course 1         2015-5-15                 tag3          tag2

 

Avatar
Discard
Best Answer

Update

I got your point. It is impossible to do it by standard instrument, you will have to change the widget (a lot of work by modifying javascript). Basically, there can't be table inside table in xml Odoo at the moment.

The only solution which I can see:

1) To modify display_name field (name_get in OpenERP 7), in order it will show all necessary data. For example:

        @api.one
        @api.depends('name', 'ref ')
        def _get_display_name(self):
            self.display_name = name + ref
        
display_name = fields.Char(string='Name', compute='_get_display_name',) name = fields.Char(string='Name') ref = fields.Char(string='Ref')

2) To use widget many2many_tags inside column of tags.

You also may try to add intermediary class, which will consists of: 1. class_x fields (inherits from this class), 2. tag as M2O field, 3. tag fields as related fields. Then you will have a tree with all the columns.

Initial answer

If I understood you in a right way, you would like to add columns from todo.task.tag model. If so, everything you need is to add tree inside tag_ids. E.g.: 

<field name="tag_ids"> <tree> <field name='name '/> <field name='parent_id '/> </tree> </field>


Avatar
Discard
Author

Your solution doesn't work in a tree view, only in a form view. I want to show different columns in a tree view for a many2many field. model Course(models.Model): name = fields.Char(string='Name Course') max_seats = fields.Integer() start_date = fields.DateTime() tag_ids = fields.Many2many('todo.task.tag') model Tag(models.Model): _name="todo.task.tag" name = fields.Char() quality = fields.Integer() In the Course's form view, I want to show two columns for the tag_ids field: name and quality: -------------------------------------------------------------------------------------------------- Name Course | max_seats | start_date | name (tag_ids) | quality (tag_ids) | -------------------------------------------------------------------------------------------------- course 1 25 2015-5-5 tag1 25 course 2 70 2015-5-6 tag2 35

Author

I have edited my original post to clarify my question.

I have added "Update" section in the answer

Related Posts Replies Views Activity
1
Mar 15
4716
1
Sep 18
11500
0
Oct 17
2876
2
Sep 17
8566
0
Apr 17
2507