django-pydantic-field

Django JSONField with Pydantic models as a Schema

OTHER License

Downloads
50.3K
Stars
111
Committers
1

Bot releases are hidden (Show)

django-pydantic-field - Make DRF's serializer SchemaField aware of `allow_null` attribute

Published by surenkov about 2 years ago

django-pydantic-field - Simplify field error handling

Published by surenkov about 2 years ago

I believe that initially introduced error_handler= param is basically doesn't make sense, as it breaks regular validation flow and could be even harmful, as by default it simply suppress errors and writes them in logging. (Re)raising validation errors would be more explicit alternative to that.

django-pydantic-field - Consider raw config dict as a valid `config=` param

Published by surenkov about 2 years ago

I feel it's an overhead to define a full Config class in some cases, like:

class Config:
    json_encoders = ...

field: t.List[Schema] = SchemaField(config=Config)

The convenient solution is to pass a dict config instead:

field: t.List[Schema] = SchemaField(config={"json_encoders": ...})

Well, it is now possible to do so.

django-pydantic-field - Explicitly state forward refs resolution behavior

Published by surenkov about 2 years ago

I'm considering current behavior with forward references resolution as expected. I.e. field schemas should be always defined before field declaration, otherwise it's not possible to bind a schema to the field during model initialzation. Note though, that annotation should still be declared as string/typing.ForwardRef, as long as prior constraint is satisfied. Main change: now such behavior is explicitly stated with tests.

django-pydantic-field - Downgrade deferred annotations support

Published by surenkov about 2 years ago

I tried to defer schema initialzation at a field descriptor level, but this approach did not succeeded with current migrations flow

django-pydantic-field - Support of direct model annotations

Published by surenkov about 2 years ago

Now, instead specifying explicit schema, one could write:

from django.db import Model
from django_pydantic_field import SchemaField

class MyModel(models.Model):
    field: list[int] = SchemaField()

The schema will be inferred at model freezing step.
Note that it's not possible to specify unresolved forward references at the moment.

django-pydantic-field - Simplified namings, initial support for model type annotations

Published by surenkov about 2 years ago

This is the first minor release of django-pydantic-field, with field-level type definitions:

class Foo(pydantic.BaseModel):
    bar: int

class MyModel(models.Model):
    meta: list[Foo] = SchemaField(schema=list[Foo])

In order to support this, I had to introduce a breaking changes in field api defintion:
all Pydantic-prefixed classes should be used without this prefix, e.g:

PydanticSchemaField -> SchemaField
PydanticSchemaRenderer -> SchemaRenderer
...
django-pydantic-field - Release v0.0.9

Published by surenkov about 2 years ago

Package Rankings
Top 11.13% on Pypi.org
Badges
Extracted from project README
PyPI Version Lint and Test Package PyPI - Downloads Supported Python Versions Supported Django Versions