Skip to content

Commit

Permalink
Merge pull request #75 from amyasnikov/fix_68
Browse files Browse the repository at this point in the history
fix ValidationError on empty form rendering
  • Loading branch information
surenkov authored Dec 20, 2024
2 parents 4c65566 + e20abe4 commit 6afa93f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
3 changes: 3 additions & 0 deletions django_pydantic_field/v2/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
11 changes: 10 additions & 1 deletion tests/v2/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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",
[
Expand Down Expand Up @@ -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()

0 comments on commit 6afa93f

Please sign in to comment.