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

Forbid callable form field defaults #301

Merged
merged 2 commits into from
Nov 10, 2023
Merged
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
14 changes: 11 additions & 3 deletions reactivated/serialization/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,13 +340,23 @@ def get_serialized_value(
else class_or_instance()
)

name = f"{value.__class__.__module__}.{value.__class__.__qualname__}"

for field in value:
if (
isinstance(field.field, django_forms.ModelChoiceField)
and schema.definitions.get("is_static_context") is True # type: ignore[comparison-overlap]
):
field.field.queryset = field.field.queryset.none()

if (
callable(field.field.initial)
and schema.definitions.get("is_static_context") is True # type: ignore[comparison-overlap]
):
assert (
False
), f"Callables are not supported in initial/default values for field {field.name} in form {name}"

form = value
context = form.get_context() # type: ignore[attr-defined]

Expand All @@ -362,9 +372,7 @@ def get_serialized_value(
value.fields = {**hidden_fields, **visible_fields}

serialized = serialize(value, schema, suppress_custom_serializer=True)
serialized[
"name"
] = f"{value.__class__.__module__}.{value.__class__.__qualname__}"
serialized["name"] = name
serialized["prefix"] = form.prefix or ""
serialized["iterator"] = list(hidden_fields.keys()) + list(
visible_fields.keys()
Expand Down
Loading