From 2490961b4b7481e480e368b75a911aa58bdad4c1 Mon Sep 17 00:00:00 2001 From: Anton M Date: Tue, 17 Dec 2024 03:07:25 +0100 Subject: [PATCH 1/2] fix ValidationError on empty form rendering --- django_pydantic_field/v2/forms.py | 3 +++ tests/v2/test_forms.py | 11 ++++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) 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..a8b07f2 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.render() From e20abe4ea9f62409e8fa05211eb1064c6bfd66d5 Mon Sep 17 00:00:00 2001 From: Anton M Date: Tue, 17 Dec 2024 03:25:02 +0100 Subject: [PATCH 2/2] fix for older django --- tests/v2/test_forms.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/v2/test_forms.py b/tests/v2/test_forms.py index a8b07f2..ea1965e 100644 --- a/tests/v2/test_forms.py +++ b/tests/v2/test_forms.py @@ -161,4 +161,4 @@ def test_annotated_acceptance(): def test_form_render_without_default(): form = NoDefaultForm() - form.render() + form.as_p()