From fe1e5d22d7ddf606c96ed9dc323f31304e8deb22 Mon Sep 17 00:00:00 2001 From: Klemen Pukl Date: Thu, 19 Dec 2024 14:53:10 +0100 Subject: [PATCH] bug fix (#267) * bug fix * AutoGeneratedField --> FieldRenderMixin, Struct * AutoGeneratedField to actual field in get_uniqueness_extra_kwargs * refactor --- dynamicforms/fields.py | 19 ++++++++++++++++++- dynamicforms/serializers.py | 11 +++++++---- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/dynamicforms/fields.py b/dynamicforms/fields.py index 46b35927..532cfc2a 100644 --- a/dynamicforms/fields.py +++ b/dynamicforms/fields.py @@ -35,7 +35,24 @@ class AutoGeneratedField(dict): - pass + def get_serializer_field(self, name, serializer, extra_params=None): + from rest_framework.utils import model_meta + + if not hasattr(serializer, "_df_model"): + serializer._df_model = getattr(serializer.Meta, "model") + if not hasattr(serializer, "_df_info"): + serializer._df_info = model_meta.get_field_info(serializer._df_model) + if not hasattr(serializer, "_df_depth"): + serializer._df_depth = getattr(serializer.Meta, "depth", 0) + + if extra_params is None: + extra_params = self + field_class, field_kwargs = serializer.build_field( + name, serializer._df_info, serializer._df_model, serializer._df_depth + ) + field_kwargs.update(extra_params) + + return field_class(**field_kwargs) class BooleanField(FieldRenderMixin, ActionMixin, FieldHelpTextMixin, ConditionalVisibilityMixin, fields.BooleanField): diff --git a/dynamicforms/serializers.py b/dynamicforms/serializers.py index ac7ac6dd..5cd93bc5 100644 --- a/dynamicforms/serializers.py +++ b/dynamicforms/serializers.py @@ -303,10 +303,14 @@ def get_uniqueness_extra_kwargs(self, field_names: List[str], declared_fields: D resolved display value in table """ for field_name in field_names[:]: - # serializer field: either what is declared as a Field instance in Serializer or AutogeneratedField - s_field = declared_fields.get(field_name, getattr(self, field_name, None)) # existing extra as provided by get_extra_kwargs extra = extra_kwargs.get(field_name, dict()) + + # serializer field: either what is declared as a Field instance in Serializer or AutogeneratedField + s_field = declared_fields.get(field_name, getattr(self, field_name, None)) + if isinstance(s_field, fields.AutoGeneratedField): + s_field = s_field.get_serializer_field(field_name, self, extra) + # model field, if it exists try: d_field = self.Meta.model._meta.get_field(field_name) @@ -350,7 +354,7 @@ def get_uniqueness_extra_kwargs(self, field_names: List[str], declared_fields: D source="*", display_form=DisplayMode.HIDDEN, display_table=DisplayMode.FULL, - render_params=s_field.render_params if s_field else None, + render_params=getattr(s_field, "render_params", None) if s_field else None, ) def get_resolve_method(fld_nm, res_fld): @@ -528,4 +532,3 @@ def determine_layout_at_runtime(self, request): except ImportError: pass return self.Meta.layout -