Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
surenkov committed Mar 30, 2024
1 parent 871af26 commit 536986f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,21 @@ class Bar(pydantic.BaseModel):
slug: str = "foo_bar"
```

In this case, exact type resolution will be postponed until initial access to the field.
Usually this happens on the first instantiation of the model.
**Pydantic v2 specific**: this behaviour is achieved by the fact that the exact type resolution will be postponed the until initial access to the field. Usually this happens on the first instantiation of the model.

To reduce the number of runtime errors related to the postponed resolution, the field itself performs a few checks against the passed schema during `./manage.py check` command invocation, and consequently, in `runserver` and `makemigrations` commands.

Here's the list of currently implemented checks:
- `pydantic.E001`: The passed schema could not be resolved. Most likely it does not exist in the scope of the defined field.
- `pydantic.E002`: `default=` value could not be serialized to the schema.
- `pydantic.W003`: The default value could not be reconstructed to the schema due to `include`/`exclude` configuration.


### `typing.Annotated` support
As of `v0.3.5`, SchemaField also supports `typing.Annotated[...]` expressions, both through `schema=` attribute or field annotation syntax; though I find the `schema=typing.Annotated[...]` variant highly discouraged.
**The current limitation** is not in the field itself, but in possible `Annotated` metadata -- practically it can contain anything, and Django migrations serializers could refuse to write it to migrations.

For most relevant types in context of Pydantic, I wrote the specific serializers (particularly for `pydantic.FieldInfo`, `pydantic.Representation` and raw dataclasses), thus it should cover the majority of `Annotated` use cases.

## Django Forms support

Expand Down
2 changes: 1 addition & 1 deletion django_pydantic_field/v2/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def check(self, **kwargs: ty.Any) -> list[checks.CheckMessage]:
if schema_default is not None:
try:
# Perform the full round-trip transformation to test the export ability.
self.adapter.validate_python(self.get_prep_value(self.default))
self.adapter.validate_python(self.get_prep_value(schema_default))
except pydantic.ValidationError as exc:
message = f"Export arguments may lead to data integrity problems. Pydantic error: \n{str(exc)}"
hint = "Please review `import` and `export` arguments."
Expand Down

0 comments on commit 536986f

Please sign in to comment.