Django JSONField with Pydantic models as a Schema
OTHER License
Bot releases are hidden (Show)
Published by surenkov about 2 years ago
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.
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.
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.
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
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.
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
...
Published by surenkov about 2 years ago