[ADD] developer/reference: add a reference page on performance

closes odoo/documentation#2794

X-original-commit: 4d8e3f9785
Signed-off-by: Antoine Vandevenne (anv) <anv@odoo.com>
Signed-off-by: Xavier Dollé (xdo) <xdo@odoo.com>
Co-authored-by: Antoine Vandevenne (anv) <anv@odoo.com>
This commit is contained in:
Xavier-Do
2021-09-30 14:53:09 +00:00
committed by Antoine Vandevenne (anv)
parent aab2d6615a
commit 46a80f60c5
17 changed files with 554 additions and 561 deletions

View File

@@ -589,32 +589,6 @@ Programming in Odoo
- As in python, use ``filtered``, ``mapped``, ``sorted``, ... methods to
ease code reading and performance.
Make your method work in batch
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
When adding a function, make sure it can process multiple records by iterating
on self to treat each record.
.. code-block:: python
def my_method(self)
for record in self:
record.do_cool_stuff()
For performance issue, when developing a 'stat button' (for instance), do not
perform a ``search`` or a ``search_count`` in a loop. It
is recommended to use ``read_group`` method, to compute all value in only one request.
.. code-block:: python
def _compute_equipment_count(self):
""" Count the number of equipment per category """
equipment_data = self.env['hr.equipment'].read_group([('category_id', 'in', self.ids)], ['category_id'], ['category_id'])
mapped_data = dict([(m['category_id'][0], m['category_id_count']) for m in equipment_data])
for category in self:
category.equipment_count = mapped_data.get(category.id, 0)
Propagate the context
~~~~~~~~~~~~~~~~~~~~~
The context is a ``frozendict`` that cannot be modified. To call a method with