[IMP] Base: resolve name when grouping by many2many
Grouping by many2many has been added with PR odoo/odoo#68958 (commit f4f570c3d6b403512d8523c74acd34d485f46af1) However the display names are not resolved resulting in the grouping displaying the actual id of the records. This also breaks some features such as `read_groups` which expect the group by data to contain a tuple (id, x) instead of just the id, this commit adds name resolving on many2many group bys. Task ID: 2398734 Part-of: odoo/odoo#74985
This commit is contained in:
committed by
Bruno Boi
parent
6c4910aac7
commit
91735098d3
@@ -310,7 +310,7 @@ class MailActivityMixin(models.AbstractModel):
|
||||
)
|
||||
self.env.cr.execute(select_query, [tz] * 3 + where_params)
|
||||
fetched_data = self.env.cr.dictfetchall()
|
||||
self._read_group_resolve_many2one_fields(fetched_data, annotated_groupbys)
|
||||
self._read_group_resolve_many2x_fields(fetched_data, annotated_groupbys)
|
||||
data = [
|
||||
{key: self._read_group_prepare_data(key, val, groupby_dict)
|
||||
for key, val in row.items()}
|
||||
|
||||
@@ -43,19 +43,19 @@ class TestM2MGrouping(common.TransactionCase):
|
||||
)
|
||||
self.assertEqual(user_by_tasks, [
|
||||
{ # first task: both users
|
||||
'task_ids': self.tasks[0].id,
|
||||
'task_ids': (self.tasks[0].id, "Super Mario Bros."),
|
||||
'task_ids_count': 2,
|
||||
'name': ['Mario', 'Luigi'],
|
||||
'__domain': [('task_ids', '=', self.tasks[0].id)],
|
||||
},
|
||||
{ # second task: Mario only
|
||||
'task_ids': self.tasks[1].id,
|
||||
'task_ids': (self.tasks[1].id, "Paper Mario"),
|
||||
'task_ids_count': 1,
|
||||
'name': ['Mario'],
|
||||
'__domain': [('task_ids', '=', self.tasks[1].id)],
|
||||
},
|
||||
{ # third task: Luigi only
|
||||
'task_ids': self.tasks[2].id,
|
||||
'task_ids': (self.tasks[2].id, "Luigi's Mansion"),
|
||||
'task_ids_count': 1,
|
||||
'name': ['Luigi'],
|
||||
'__domain': [('task_ids', '=', self.tasks[2].id)],
|
||||
@@ -71,13 +71,13 @@ class TestM2MGrouping(common.TransactionCase):
|
||||
)
|
||||
self.assertEqual(task_by_users, [
|
||||
{ # task of Mario
|
||||
'user_ids': self.users[0].id,
|
||||
'user_ids': (self.users[0].id, "Mario"),
|
||||
'user_ids_count': 1,
|
||||
'name': ["Super Mario Bros."],
|
||||
'__domain': ['&', ('user_ids', '=', self.users[0].id), ('id', '=', self.tasks[0].id)],
|
||||
},
|
||||
{ # task of Luigi
|
||||
'user_ids': self.users[1].id,
|
||||
'user_ids': (self.users[1].id, "Luigi"),
|
||||
'user_ids_count': 1,
|
||||
'name': ["Super Mario Bros."],
|
||||
'__domain': ['&', ('user_ids', '=', self.users[1].id), ('id', '=', self.tasks[0].id)],
|
||||
@@ -92,13 +92,13 @@ class TestM2MGrouping(common.TransactionCase):
|
||||
)
|
||||
self.assertEqual(task_by_users, [
|
||||
{ # tasks of Mario
|
||||
'user_ids': self.users[0].id,
|
||||
'user_ids': (self.users[0].id, "Mario"),
|
||||
'user_ids_count': 2,
|
||||
'name': unordered(["Super Mario Bros.", "Paper Mario"]),
|
||||
'__domain': [('user_ids', '=', self.users[0].id)],
|
||||
},
|
||||
{ # tasks of Luigi
|
||||
'user_ids': self.users[1].id,
|
||||
'user_ids': (self.users[1].id, "Luigi"),
|
||||
'user_ids_count': 2,
|
||||
'name': unordered(["Super Mario Bros.", "Luigi's Mansion"]),
|
||||
'__domain': [('user_ids', '=', self.users[1].id)],
|
||||
@@ -151,13 +151,13 @@ class TestM2MGrouping(common.TransactionCase):
|
||||
)
|
||||
self.assertEqual(as_admin, [
|
||||
{ # tasks of Mario
|
||||
'user_ids': self.users[0].id,
|
||||
'user_ids': (self.users[0].id, "Mario"),
|
||||
'user_ids_count': 2,
|
||||
'name': unordered(["Super Mario Bros.", "Paper Mario"]),
|
||||
'__domain': [('user_ids', '=', self.users[0].id)],
|
||||
},
|
||||
{ # tasks of Luigi
|
||||
'user_ids': self.users[1].id,
|
||||
'user_ids': (self.users[1].id, "Luigi"),
|
||||
'user_ids_count': 2,
|
||||
'name': unordered(["Super Mario Bros.", "Luigi's Mansion"]),
|
||||
'__domain': [('user_ids', '=', self.users[1].id)],
|
||||
@@ -203,7 +203,7 @@ class TestM2MGrouping(common.TransactionCase):
|
||||
)
|
||||
self.assertEqual(as_demo, [
|
||||
{ # tasks of Mario
|
||||
'user_ids': self.users[0].id,
|
||||
'user_ids': (self.users[0].id, "Mario"),
|
||||
'user_ids_count': 2,
|
||||
'name': unordered(['Super Mario Bros.', 'Paper Mario']),
|
||||
'__domain': [('user_ids', '=', self.users[0].id)],
|
||||
|
||||
+7
-7
@@ -2334,7 +2334,7 @@ class BaseModel(metaclass=MetaModel):
|
||||
# full domain for this groupby spec
|
||||
d = None
|
||||
if value:
|
||||
if ftype == 'many2one':
|
||||
if ftype in ['many2one', 'many2many']:
|
||||
value = value[0]
|
||||
elif ftype in ('date', 'datetime'):
|
||||
locale = get_lang(self.env).code
|
||||
@@ -2570,7 +2570,7 @@ class BaseModel(metaclass=MetaModel):
|
||||
if not groupby_fields:
|
||||
return fetched_data
|
||||
|
||||
self._read_group_resolve_many2one_fields(fetched_data, annotated_groupbys)
|
||||
self._read_group_resolve_many2x_fields(fetched_data, annotated_groupbys)
|
||||
|
||||
data = [{k: self._read_group_prepare_data(k, v, groupby_dict) for k, v in r.items()} for r in fetched_data]
|
||||
|
||||
@@ -2597,12 +2597,12 @@ class BaseModel(metaclass=MetaModel):
|
||||
)
|
||||
return result
|
||||
|
||||
def _read_group_resolve_many2one_fields(self, data, fields):
|
||||
many2onefields = {field['field'] for field in fields if field['type'] == 'many2one'}
|
||||
for field in many2onefields:
|
||||
def _read_group_resolve_many2x_fields(self, data, fields):
|
||||
many2xfields = {field['field'] for field in fields if field['type'] in ['many2one', 'many2many']}
|
||||
for field in many2xfields:
|
||||
ids_set = {d[field] for d in data if d[field]}
|
||||
m2o_records = self.env[self._fields[field].comodel_name].browse(ids_set)
|
||||
data_dict = dict(lazy_name_get(m2o_records.sudo()))
|
||||
m2x_records = self.env[self._fields[field].comodel_name].browse(ids_set)
|
||||
data_dict = dict(lazy_name_get(m2x_records.sudo()))
|
||||
for d in data:
|
||||
d[field] = (d[field], data_dict[d[field]]) if d[field] else False
|
||||
|
||||
|
||||
Reference in New Issue
Block a user