Skip to main content

Django JSONField with Pydantic models as a Schema

Project description

PyPI Version Lint and Test Package Downloads Supported Python Versions Supported Django Versions

Django + Pydantic = 🖤

Django JSONField with Pydantic models as a Schema

Usage

Install the package with pip install django-pydantic-field.

import pydantic
from datetime import date
from uuid import UUID

from django.db import models
from django_pydantic_field import SchemaField


class Foo(pydantic.BaseModel):
    count: int
    size: float = 1.0


class Bar(pydantic.BaseModel):
    slug: str = "foo_bar"


class MyModel(models.Model):
    # Infer schema from field annotation
    foo_field: Foo = SchemaField()

    # or explicitly pass schema to the field
    bar_list: typing.Sequence[Bar] = SchemaField(schema=list[Bar])

    # Pydantic exportable types are supported
    raw_date_map: dict[int, date] = SchemaField()
    raw_uids: set[UUID] = SchemaField()

...
    
model = MyModel(
    foo_field={"count": "5"},
    bar_list=[{}],
    raw_date_map={1: "1970-01-01"},
    raw_uids={"17a25db0-27a4-11ed-904a-5ffb17f92734"}
)
model.save()

assert model.foo_field == Foo(count=5, size=1.0)
assert model.bar_list == [Bar(slug="foo_bar")]
assert model.raw_date_map == {1: date(1970, 1, 1)}
assert model.raw_uids == {UUID("17a25db0-27a4-11ed-904a-5ffb17f92734")}

Practically, schema could be of any type supported by Pydantic. In addition, an external config class can be passed for such schemes.

Forward referencing annotations

It is also possible to use SchemaField with forward references and string literals, e.g the code below is also valid:

class MyModel(models.Model):
    foo_field: "Foo" = SchemaField()
    bar_list: typing.Sequence["Bar"] = SchemaField(schema=typing.ForwardRef("list[Bar]"))


class Foo(pydantic.BaseModel):
    count: int
    size: float = 1.0


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.

Django Forms support

It is possible to create Django forms, which would validate against the given schema:

from django import forms
from django_pydantic_field.forms import SchemaField


class Foo(pydantic.BaseModel):
    slug: str = "foo_bar"


class FooForm(forms.Form):
    field = SchemaField(Foo)  # `typing.ForwardRef("Foo")` is fine too, but only in Django 4+


form = FooForm(data={"field": '{"slug": "asdf"}'})
assert form.is_valid()
assert form.cleaned_data["field"] == Foo(slug="asdf")

django_pydantic_field also supports auto-generated fields for ModelForm and modelform_factory:

class FooModelForm(forms.ModelForm):
    class Meta:
        model = Foo
        fields = ["field"]

form = FooModelForm(data={"field": '{"slug": "asdf"}'})
assert form.is_valid()
assert form.cleaned_data["field"] == Foo(slug="asdf")

...

# ModelForm factory support
AnotherFooModelForm = modelform_factory(Foo, fields=["field"])
form = AnotherFooModelForm(data={"field": '{"slug": "bar_baz"}'})

assert form.is_valid()
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 REST Framework support

from rest_framework import generics, serializers
from django_pydantic_field.rest_framework import SchemaField, AutoSchema


class MyModelSerializer(serializers.ModelSerializer):
    foo_field = SchemaField(schema=Foo)

    class Meta:
        model = MyModel
        fields = '__all__'


class SampleView(generics.RetrieveAPIView):
    serializer_class = MyModelSerializer

    # optional support of OpenAPI schema generation for Pydantic fields
    schema = AutoSchema()

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 SchemaRenderer, SchemaParser, AutoSchema


@api_view(["POST"])
@parser_classes([SchemaParser[Foo]]):
@renderer_classes([SchemaRenderer[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 = [SchemaParser[Foo]]
    renderer_classes = [SchemaRenderer[list[Foo]]]

    # optional support of OpenAPI schema generation for Pydantic parsers/renderers
    schema = AutoSchema()

    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])

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

django-pydantic-field-0.2.5.tar.gz (19.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_pydantic_field-0.2.5-py3-none-any.whl (14.3 kB view details)

Uploaded Python 3

File details

Details for the file django-pydantic-field-0.2.5.tar.gz.

File metadata

  • Download URL: django-pydantic-field-0.2.5.tar.gz
  • Upload date:
  • Size: 19.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.17

File hashes

Hashes for django-pydantic-field-0.2.5.tar.gz
Algorithm Hash digest
SHA256 a084f501baedf508f60297080cadef3dc1f30aebdae119dac6472f256fa53f8c
MD5 73fe9f12074e287a362e5a40c7e5fc9c
BLAKE2b-256 8261044c32b9c6bf5f18b9a9cb8d879e21627c0aafc64d65c1b82e62439ddbcf

See more details on using hashes here.

File details

Details for the file django_pydantic_field-0.2.5-py3-none-any.whl.

File metadata

File hashes

Hashes for django_pydantic_field-0.2.5-py3-none-any.whl
Algorithm Hash digest
SHA256 f458a0f43299586cdf2975d464a03ec22a7adeaf4888f56376e225c1411d61e7
MD5 ad11b3b48667d8d99a9946ab7e2cc9f2
BLAKE2b-256 a8f92536125f14296a48039b3667f2ac616d8f5b9ecb98b884ac38f9559ab71a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page