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 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):
foo_field: Foo = SchemaField(schema=Foo)
bar_list: list[Bar] = SchemaField(schema=list[Bar])
raw_date_map: dict[date, int] = SchemaField(schema=dict[date, int])
raw_uids: set[UUID] = SchemaField(schema=set[UUID])
...
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_uid_set = {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.
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
Built Distribution
Close
Hashes for django-pydantic-field-0.1.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 62198b6174d23c5fa5f97ceaaef999273b1f4db1d905f64dee97eceff48f54be |
|
MD5 | dc2def489d2d5d0dc9e4926cffdc61d5 |
|
BLAKE2b-256 | 6beeb04775eb3024cc1d596e73a94916dcd28cc8f51f57c8310616328359b826 |
Close
Hashes for django_pydantic_field-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 40f02f8d7805ef451f0f9fb786415cac875903e74ab91dc1d3a9b32993ca6bc5 |
|
MD5 | 7b928e931eaac197c37f67c96a3f91a3 |
|
BLAKE2b-256 | 7a7d6d9d77aa1b4b85e38a281be0276513569e79d35d8a0190849630e3bea81a |