Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug fix #267

Merged
merged 4 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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

Loading