Files
odoo_source/odoo/addons/test_read_group/models.py
T
Rémy Voet (ryv) cdebf336a5 [REF] core: _read_group for backend use
The `_read_group` was designed to be used by the web client to
efficiently compute aggregations grouped by one or more fields.
However, more and more developers have been using it from the backend
to make computations more efficient (avoid doing the aggregation
in Python). Unfortunately, the API was designed for the web client,
which added a lot of boilerplate when used in the Python (list of
dict with misleading key name choices).

`_read_group` was created to improve the performance of read_group
for backend use (4ef0c00b4b), but didn't
change the API and based the implementation on read_group itself.

Rewrite `_read_group` from scratch with a new API to make it easier
to use from the backend (see the method documentation). Also, split
the method to make it easy to override and add custom behavior.

Part-of: odoo/odoo#110737
2023-04-19 21:58:26 +02:00

105 lines
2.9 KiB
Python

# -*- coding: utf-8 -*-
from odoo import fields, models
class GroupOnDate(models.Model):
_name = 'test_read_group.on_date'
_description = 'Group Test Read On Date'
date = fields.Date("Date")
value = fields.Integer("Value")
class BooleanAggregate(models.Model):
_name = 'test_read_group.aggregate.boolean'
_description = 'Group Test Read Boolean Aggregate'
_order = 'key DESC'
key = fields.Integer()
bool_and = fields.Boolean(default=False, group_operator='bool_and')
bool_or = fields.Boolean(default=False, group_operator='bool_or')
bool_array = fields.Boolean(default=False, group_operator='array_agg')
class Aggregate(models.Model):
_name = 'test_read_group.aggregate'
_order = 'id'
_description = 'Group Test Aggregate'
key = fields.Integer()
value = fields.Integer("Value")
partner_id = fields.Many2one('res.partner')
display_name = fields.Char()
# we use a selection that is in reverse lexical order, in order to check the
# possible reordering made by read_group on selection fields
SELECTION = [('c', "C"), ('b', "B"), ('a', "A")]
class GroupOnSelection(models.Model):
_name = 'test_read_group.on_selection'
_description = 'Group Test Read On Selection'
state = fields.Selection([('a', "A"), ('b', "B")], group_expand='_expand_states')
static_expand = fields.Selection(SELECTION, group_expand=True)
dynamic_expand = fields.Selection(lambda self: SELECTION, group_expand=True)
no_expand = fields.Selection(SELECTION)
value = fields.Integer()
def _expand_states(self, states, domain, order):
# return all possible states, in order
return [key for key, val in type(self).state.selection]
class FillTemporal(models.Model):
_name = 'test_read_group.fill_temporal'
_description = 'Group Test Fill Temporal'
date = fields.Date()
datetime = fields.Datetime()
value = fields.Integer()
class Order(models.Model):
_name = 'test_read_group.order'
_description = 'Sales order'
line_ids = fields.One2many('test_read_group.order.line', 'order_id')
class OrderLine(models.Model):
_name = 'test_read_group.order.line'
_description = 'Sales order line'
order_id = fields.Many2one('test_read_group.order', ondelete='cascade')
value = fields.Integer()
class User(models.Model):
_name = 'test_read_group.user'
_description = "User"
name = fields.Char(required=True)
task_ids = fields.Many2many(
'test_read_group.task',
'test_read_group_task_user_rel',
'user_id',
'task_id',
string="Tasks",
)
class Task(models.Model):
_name = 'test_read_group.task'
_description = "Project task"
name = fields.Char(required=True)
user_ids = fields.Many2many(
'test_read_group.user',
'test_read_group_task_user_rel',
'task_id',
'user_id',
string="Collaborators",
)