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])
raw_date_map = PydanticSchemaField(schema=dict[datetime.date, int])
raw_uids = PydanticSchemaField(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: datetime.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 (
PydanticSchemaField,
PydanticAutoSchema,
)
class MyModelSerializer(serializers.ModelSerializer):
foo_field = PydanticSchemaField(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 = PydanticAutoSchema()
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,
PydanticAutoSchema,
)
@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]]]
# optional support of OpenAPI schema generation for Pydantic parsers/renderers
schema = PydanticAutoSchema()
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.0.9.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4602199384287a18cd9f2f885b5e105e6618983951ebd24bc5f7d913ec319161 |
|
MD5 | 1c9b28fc1f9692f29fff21fbfc3b1346 |
|
BLAKE2b-256 | 18da322ef45ea2805213a668e326f456791c71cceea544ffb9193bbb933a7f94 |
Close
Hashes for django_pydantic_field-0.0.9-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e1ef0ed4294eaeb027ab911555a65fb77ebfcc4fd3d442dc7b45d093f91f87af |
|
MD5 | 11565e700ce8c6e27edbe7ccdf52bae8 |
|
BLAKE2b-256 | 366f982ec25241b85869770831e5f8557dc0e5f77e022b447f9292c414688e94 |