Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Brontes committed Dec 18, 2024
1 parent 60f969b commit b7d4902
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 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
17 changes: 1 addition & 16 deletions dynamicforms/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,29 +302,14 @@ def get_uniqueness_extra_kwargs(self, field_names: List[str], declared_fields: D
Finds all ChoiceFields and RelatedFields and creates additional "-display" fields to show
resolved display value in table
"""
from rest_framework.utils import model_meta

model = getattr(self.Meta, "model")
depth = getattr(self.Meta, "depth", 0)
info = None

def _get_info():
nonlocal info
if info is None:
info = model_meta.get_field_info(model)
return info

for field_name in field_names[:]:
# 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):
field_class, field_kwargs = self.build_field(field_name, _get_info(), model, depth)
field_kwargs.update(extra)

s_field = field_class(**field_kwargs)
s_field = s_field.get_serializer_field(field_name, self, extra)

# model field, if it exists
try:
Expand Down

0 comments on commit b7d4902

Please sign in to comment.