[IMP] fields: write only once per Image field

Before this commit, flushing a model that uses the `image.mixin` would generate
twice as many queries as it should.

This is because `_compute_related` is not meant to be overridden. In the
override of `Image` we call `super()` then post process the records by
reassigning each record's relevant image field with `_image_process`.

The `super()` will assign the field of the records once which
will in turn trigger a `write` (and thus, will generate queries) then
after the `super()` call we reassign them which will re-trigger the same
`write` and the same queries.

With this commit, instead of overriding `_compute_related` we extract the
processing to a method that can be overridden, so that the `write` is called
simply once.

The result, obviously, is that queries related to the `write` are cut in half.

Related PR: #36683 & #36288

Co-authored-by: Adrian Torres <adt@odoo.com>
Co-authored-by: Sébastien Theys <seb@odoo.com>
This commit is contained in:
Sébastien Theys
2019-09-23 20:55:39 +00:00
co-authored by Adrian Torres
parent 50c7941fa6
commit 9112972e15
+8 -5
View File
@@ -628,7 +628,11 @@ class Field(MetaField('DummyField', (object,), {})):
)
# assign final values to records
for record, value in zip(records, values):
record[self.name] = value[self.related_field.name]
record[self.name] = self._process_related(value[self.related_field.name])
def _process_related(self, value):
"""No transformation by default, but allows override."""
return value
def _inverse_related(self, records):
""" Inverse the related field ``self`` on ``records``. """
@@ -2002,10 +2006,9 @@ class Image(Binary):
value = image_process(value, size=(self.max_width, self.max_height))
return value
def _compute_related(self, records):
super(Image, self)._compute_related(records)
for record in records:
record[self.name] = self._image_process(record[self.name])
def _process_related(self, value):
"""Override to resize the related value before saving it on self."""
return self._image_process(super()._process_related(value))
class Selection(Field):