diff --git a/django_pydantic_field/v2/forms.py b/django_pydantic_field/v2/forms.py index 4f12c1a..2ebef81 100644 --- a/django_pydantic_field/v2/forms.py +++ b/django_pydantic_field/v2/forms.py @@ -80,6 +80,9 @@ def to_python(self, value: ty.Any) -> ty.Any: return value def prepare_value(self, value): + if value is None: + return None + if isinstance(value, InvalidJSONInput): return value diff --git a/tests/v2/test_forms.py b/tests/v2/test_forms.py index 64b1b33..ea1965e 100644 --- a/tests/v2/test_forms.py +++ b/tests/v2/test_forms.py @@ -9,7 +9,7 @@ from django.forms import Form, modelform_factory from tests.conftest import InnerSchema -from tests.test_app.models import SampleForwardRefModel, SampleSchema +from tests.test_app.models import SampleForwardRefModel, SampleSchema, ExampleSchema fields = pytest.importorskip("django_pydantic_field.v2.fields") forms = pytest.importorskip("django_pydantic_field.v2.forms") @@ -19,6 +19,10 @@ class SampleForm(Form): field = forms.SchemaField(ty.ForwardRef("SampleSchema")) +class NoDefaultForm(Form): + field = forms.SchemaField(schema=ExampleSchema) + + @pytest.mark.parametrize( "raw_data, clean_data", [ @@ -153,3 +157,8 @@ def test_annotated_acceptance(): field = forms.SchemaField(te.Annotated[InnerSchema, pydantic.Field(title="Inner Schema")]) value = InnerSchema.model_validate({"stub_str": "abc", "stub_list": ["1970-01-01"]}) assert field.prepare_value(value) + + +def test_form_render_without_default(): + form = NoDefaultForm() + form.as_p()