This question has been flagged
1 Reply
10584 Views

I want to group by computed field (type_client).

I know that I should make it store = True, but I cannot because the values ​​are dynamic. Is there another option?

This is my function:


def act_show_supect(self):
    for objctf in self:
        for client in self.env['res.partner'].search([('user_id.id', '=', self.user_id.id),
                                                      ('company_type', '=', 'company'),
                                                      ('type_client', '=', 'suspect')]):
            if client.type_client == 'suspect':
                objctf.ensure_one()
                res = objctf.env['ir.actions.act_window'].for_xml_id(
                    'base', 'action_partner_form')
                res.update(
                    context=dict(
                        objctf.env.context,
                        search_default_user_id_id=objctf.user_id.id,
                        search_default_type_client='suspect',
                    ),
                    domain=[('user_id.id', '=', objctf.user_id.id),
                            ('company_type', '=', 'company'),
                            ('type_client', '=', 'suspect')]
                )

                return res

And this is the error after execution:

File "E:\odoo11.0\odoo\models.py", line 1908, in read_group
    result = self._read_group_raw(domain, fields, groupby, offset=offset, 
    limit=limit, orderby=orderby, lazy=lazy)

  File "E:\odoo11.0\odoo\models.py", line 1946, in _read_group_raw
    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

And i want to group by:

('type_client', '=', 'suspect')

Avatar
Discard
Best Answer

 Group by, filters, searches assume to work only with storable fields, since such operations assume direct queries to database. If a field is not stored, it is not kept in a database. So, a simple answer is no, it is not possible.

What you can do:

  1. To prepare a cron, which would calculate your fields regularly. So, it would be stored fields. The drawback is obvious: in such a case it would not be a computed field, it would not be calculated in real time. Thus, there might be obsolescent data which is not yet updated

  2. To implement a wizard, where you would re-calculate your fields and after that open a view with groupings (or a report). I guess, it is the most efficient approach, since would not mislead users.

  3. To  re-write methods of the models.py which relate to search and groupings. Also you can experiment with a 'search' attribute of the field and related search methods. It would be a really difficult task, and, in the most cases, would lead to performance slowdowns.



Avatar
Discard