From 76e5c6af2a35bbc8e8d33832e78d214b3bbddd7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Voet=20=28ryv=29?= Date: Wed, 17 Apr 2024 16:31:11 +0200 Subject: [PATCH] [FIX] core: flush non-attachment binary fields when necessary Non-attachment binary fields need to be flushed before reading their size, since the latter relies on the database's binary size function. Part-of: odoo/odoo#160708 --- odoo/addons/test_new_api/tests/test_new_fields.py | 3 +++ odoo/models.py | 13 +++++++++---- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/odoo/addons/test_new_api/tests/test_new_fields.py b/odoo/addons/test_new_api/tests/test_new_fields.py index a696b1f47ad..c562742540c 100644 --- a/odoo/addons/test_new_api/tests/test_new_fields.py +++ b/odoo/addons/test_new_api/tests/test_new_fields.py @@ -2707,6 +2707,9 @@ class TestFields(TransactionCaseWithUserDemo): self.assertEqual(record_bin_size.image, b'31.54 Kb') self.assertEqual(record_bin_size.image_512, b'1.02 Kb') self.assertEqual(record_bin_size.image_256, b'424.00 bytes') + # non-attachment binary fields: value returned as str in a different + # form, because coming from PostgreSQL instead of filestore + self.assertEqual(record_bin_size.image_64, '148 bytes') # ensure image_data_uri works (value must be bytes and not string) self.assertEqual(record.image_256[:8], b'iVBORw0K') diff --git a/odoo/models.py b/odoo/models.py index 48868dbea44..05ef544da6c 100644 --- a/odoo/models.py +++ b/odoo/models.py @@ -3893,11 +3893,16 @@ class BaseModel(metaclass=MetaModel): (column_fields if field.column_type else other_fields).add(field) # necessary to retrieve the en_US value of fields without a translation - translated_field_names = [field.name for field in column_fields if field.translate] - if translated_field_names: - self.flush_model(translated_field_names) - context = self.env.context + field_names_to_flush = [ + field.name for field in column_fields + if field.translate or ( + field.type == 'binary' + and (context.get('bin_size') or context.get('bin_size_' + field.name)) + ) + ] + if field_names_to_flush: + self.flush_model(field_names_to_flush) if column_fields: # the query may involve several tables: we need fully-qualified names