[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
This commit is contained in:
Rémy Voet (ryv)
2024-04-26 17:17:52 +00:00
committed by Raphael Collet
parent b890049fda
commit 76e5c6af2a
2 changed files with 12 additions and 4 deletions
@@ -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')
+9 -4
View File
@@ -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