Skip to content

Commit

Permalink
bug fix (#267)
Browse files Browse the repository at this point in the history
* bug fix

* AutoGeneratedField --> FieldRenderMixin, Struct

* AutoGeneratedField to actual field in get_uniqueness_extra_kwargs

* refactor
  • Loading branch information
Brontes authored Dec 19, 2024
1 parent e9dee71 commit fe1e5d2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
19 changes: 18 additions & 1 deletion dynamicforms/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
11 changes: 7 additions & 4 deletions dynamicforms/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -528,4 +532,3 @@ def determine_layout_at_runtime(self, request):
except ImportError:
pass
return self.Meta.layout

0 comments on commit fe1e5d2

Please sign in to comment.