Django JSONField with Pydantic models as a Schema
Project description
Django + Pydantic = 🖤
Django JSONField with Pydantic models as a Schema
Usage
Install the package with pip install django-pydantic-field
.
import pydantic
from django.db import models
from django_pydantic_field import PydanticSchemaField
class Foo(pydantic.BaseModel):
count: int
size: float = 1.0
class Bar(pydantic.BaseModel):
slug: str = "foo_bar"
class MyModel(models.Model):
foo_field = PydanticSchemaField(schema=Foo)
bar_list = PydanticSchemaField(schema=list[Bar])
...
model = MyModel(foo_field={"count": "5"}, bar_list=[{}])
model.save()
assert model.foo_field == Foo(count=5, size=1.0)
assert model.bar_list == [Bar(slug="foo_bar")]
Django REST Framework support
from rest_framework import serializers
from django_pydantic_field.rest_framework import PydanticSchemaField
class MyModelSerializer(serializers.ModelSerializer):
foo_field = PydanticSchemaField(schema=Foo)
class Meta:
model = MyModel
fields = '__all__'
Global approach with typed parser
and renderer
classes
from rest_framework import views
from rest_framework.decorators import api_view, parser_classes, renderer_classes
from django_pydantic_field.rest_framework import PydanticSchemaRenderer, PydanticSchemaParser
@api_view(["POST"])
@parser_classes([PydanticSchemaParser[Foo]]):
@renderer_classes([PydanticSchemaRenderer[list[Foo]]])
def foo_view(request):
assert isinstance(request.data, Foo)
count = request.data.count + 1
return Response([Foo(count=count)])
class FooClassBasedView(views.APIView):
parser_classes = [PydanticSchemaParser[Foo]]
renderer_classes = [PydanticSchemaRenderer[list[Foo]]]
def get(self, request, *args, **kwargs):
assert isinstance(request.data, Foo)
return Response([request.data])
def put(self, request, *args, **kwargs):
assert isinstance(request.data, Foo)
count = request.data.count + 1
return Response([request.data])
Caveats
-
Built-in generic annotations introduced in Python 3.9 are expecting to fail during
manage.py makemigrations
step: due to how Django treats field serialization/reconstruction while writing migrations, it is not possible to create a custom serializer to distinguish between types.GenericAlias and a type instance (any class) without pushing a patch directly in Django.A workaround is to use generic collections from
typing
module, even though they're marked as deprecated and will be eventually removed in the future versions of Python.Note, that this restriction applies only for
PydanticSchemaField
. DRF integrations sould work fine though.
Acknowledgement
- Churkin Oleg for his Gist as a source of inspiration;
- Boutique Air Flight Operations platform as a test ground;
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for django-pydantic-field-0.0.5.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7dd873b72fcb436d09db0697cd8a8396bf35efa7656d3ea12379af2971624baa |
|
MD5 | 216ce67e6ceb3aab9c8a46ecd72bb243 |
|
BLAKE2b-256 | 869cce7072f5092e441a5cc7c8cccf02d5850ab9bd8d97ddfabdfd6f1b181e66 |
Hashes for django_pydantic_field-0.0.5-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 212038d97c182dda845e0c66ffb5adf926c1c3bf15cbc534789a85fad1d1bf48 |
|
MD5 | 4bd45dec0e5f71811a1ce0618ad253de |
|
BLAKE2b-256 | e3b5ca5f0a4b1129bbf8dc32d3ba5f21eadf4a8744b1c0c64a9109f80f3bdcc7 |