diff --git a/README.md b/README.md index 288af4c..ee03cfd 100644 --- a/README.md +++ b/README.md @@ -144,6 +144,32 @@ assert form.cleaned_data["field"] == Foo(slug="bar_baz") Note, that forward references would be resolved until field is being bound to the form instance. +### `django-jsonform` widgets +[`django-jsonform`](https://django-jsonform.readthedocs.io) offers a dynamic form construction based on the specified JSONSchema. +`django_pydantic_field.forms.SchemaField` plays nicely with its widgets, but only for Pydantic v2: + +``` python +from django_pydantic_field.forms import SchemaField +from django_jsonform.widgets import JSONFormWidget + +class FooForm(forms.Form): + field = SchemaField(Foo, widget=JSONFormWidget) +``` + +It is also possible to override the default form widget for Django Admin site, without writing custom admin forms: + +``` python +from django.contrib import admin +from django_pydantic_field.v2.fields import PydanticSchemaField # NOTE: Importing direct field class instead of `SchemaField` wrapper. +from django_jsonform.widgets import JSONFormWidget + +@admin.site.register(SchemaModel) +class SchemaModelAdmin(admin.ModelAdmin): + formfield_overrides = { + PydanticSchemaField: {"widget": JSONFormWidget}, + } +``` + ## Django REST Framework support ``` python diff --git a/django_pydantic_field/v2/fields.py b/django_pydantic_field/v2/fields.py index 5543ee8..7113342 100644 --- a/django_pydantic_field/v2/fields.py +++ b/django_pydantic_field/v2/fields.py @@ -116,13 +116,12 @@ def check(self, **kwargs: ty.Any) -> list[checks.CheckMessage]: message = f"Cannot resolve the schema. Original error: \n{exc.args[0]}" performed_checks.append(checks.Error(message, obj=self, id="pydantic.E001")) - if self.has_default(): - try: - # Test that the default value conforms to the schema. - self.get_prep_value(self.get_default()) - except pydantic.ValidationError as exc: - message = f"Default value cannot be adapted to the schema. Pydantic error: \n{str(exc)}" - performed_checks.append(checks.Error(message, obj=self, id="pydantic.E002")) + try: + # Test that the default value conforms to the schema. + self.get_prep_value(self.get_default()) + except pydantic.ValidationError as exc: + message = f"Default value cannot be adapted to the schema. Pydantic error: \n{str(exc)}" + performed_checks.append(checks.Error(message, obj=self, id="pydantic.E002")) if {"include", "exclude"} & self.export_kwargs.keys(): # Try to prepare the default value to test export ability against it.