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

Hello everyone,

I am trying to create a group for my search view which includes a many2many field.
When I try to load the "group by" option I have created for this many2many field, I get the error:

assert gb_field.store and gb_field.column_type, "Fields in 'groupby' must be regular database-persisted fields (no function or related fields), or function fields with store=True"
AssertionError: Fields in 'groupby' must be regular database-persisted fields (no function or related fields), or function fields with store=True

My many2many field is declared as:
mechanic_ids = fields.Many2many('res.users', relation='timesheet_rel_users', column1='id',column2='name') 

On XML for the search view I have:

The above error is raised when I try to load this specific "group by" option.

Is there any way I can create a "group by" option which includes a many2many field?

Thank you in advance

Best regards

Paulo

Avatar
Discard
Best Answer

It is not possible to group by many2many field.

In that case the line might be shown on the Odoo interface more than one time, what is not acceptable.

As alternative you can create a many2one field which would compute the 'main' record from many2many. For example, you can group partner by tags, but you can group by some computed 'main tag'.

Besides, search by many2many field would work. An example from res.partner: 

<field name="category_id" string="Tag" filter_domain="[('category_id','ilike', self)]"/>

Also you may generate some custom report for your model, which would join tables in the way you like (SQL statement).

UPDATE

@api.multi
@api.depends('mechanic_ids')
def _compute_mynewfield_id(self):
for record in self:
record.mynewfield_id = record.mechanic_ids and record.mechanic_ids[0] or False


mynewfield_id = fields.Many2one('mechanic_ids', compute=_compute_mynewfield_id, store=True) # it is possible to search only among stored fields

Avatar
Discard
Author

Hi @Odoo Tools,

Thank you very much once again for your help on my path on learning Odoo development.

I have tried to create a "computed" field for this purpose I was unable to do it.

Can you please help me with a sample code?

My many2many field is declared as:

myfield_ids = fields.Many2many('res.users', relation='timesheet_rel_users', column1='id',column2='name')

I have tried to create a many2one computed field declared as:

mynewfield_id = fields.Many2one('mechanic_ids', compute='_get_values, store=False)

My incomplete function:

@api.one

@api.depends('mechanic_ids')

def _get_values(self):

value = self.mechanic_ids

for record in value:

I am stuck from this point forward.

From here, If I print the "value", I can get the desired value for every and each record, but do not know how to complete the function and pass it to XML view filter.

Thank you once again

Best regards

Paulo

see the update

Author

Dear @Odoo Tools,

Thank you very very much once again.

+1000 for you

Best regards

Paulo

Related Posts Replies Views Activity
2
Jul 19
9760
1
Jun 19
5963
0
Feb 23
1489
1
Aug 21
9217
1
Aug 24
5859