Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
surenkov committed Apr 9, 2024
1 parent 3104235 commit 0921dd3
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,34 @@ 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_jsonform.widgets import JSONFormWidget

# NOTE: Importing direct field class instead of `SchemaField` wrapper.
from django_pydantic_field.v2.fields import PydanticSchemaField

@admin.site.register(SchemaModel)
class SchemaModelAdmin(admin.ModelAdmin):
formfield_overrides = {
PydanticSchemaField: {"widget": JSONFormWidget},
}
```

## Django REST Framework support

``` python
Expand Down
13 changes: 6 additions & 7 deletions django_pydantic_field/v2/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 0921dd3

Please sign in to comment.