From 946a13cbada8f3af902644f7fe2b4a1f4db8cae0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lubo=C5=A1=20M=C3=A1tl?= Date: Thu, 15 Dec 2022 19:01:26 +0100 Subject: [PATCH] Fixe changed fields for file field --- chamber/models/changed_fields.py | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/chamber/models/changed_fields.py b/chamber/models/changed_fields.py index 1233640..d52a9ef 100644 --- a/chamber/models/changed_fields.py +++ b/chamber/models/changed_fields.py @@ -5,8 +5,8 @@ from chamber.utils.decorators import singleton -def _should_exclude_field(field, fields, exclude): - return (fields and field.name not in fields) or (exclude and field.name in exclude) +def _should_exclude_field(field_name, fields, exclude): + return (fields and field_name not in fields) or (exclude and field_name in exclude) def field_value_from_instance(field, instance): @@ -46,12 +46,19 @@ def __bool__(self): Deferred = DeferredSingleton() -def unknown_model_fields_to_dict(instance, fields=None, exclude=None): +def get_model_fields(model): + return [field for field in model._meta.concrete_fields] # pylint: disable=W0212 + + +def get_model_field_names(model): + return [field.name for field in get_model_fields(model)] + +def unknown_model_fields_to_dict(instance, fields=None, exclude=None): return { - field.name: Unknown - for field in instance._meta.concrete_fields # pylint: disable=W0212 - if not _should_exclude_field(field, fields, exclude) + field_name: Unknown + for field_name in get_model_field_names(instance) + if not _should_exclude_field(field_name, fields, exclude) } @@ -60,9 +67,9 @@ def model_to_dict(instance, fields=None, exclude=None): The same implementation as django model_to_dict but editable fields are allowed """ return { - field.name: field_value_from_instance(field, instance) - for field in instance._meta.concrete_fields # pylint: disable=W0212 - if not _should_exclude_field(field, fields, exclude) + field.name: copy.deepcopy(field_value_from_instance(field, instance)) + for field in get_model_fields(instance) # pylint: disable=W0212 + if not _should_exclude_field(field.name, fields, exclude) }