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_uid_set={"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 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])
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.8.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7869ab17489324190cd3ea05aeec80cc15ac73211fdfb77731efbeaa8e0ea2a2 |
|
MD5 | fee49f2ec6b284ad334d147dd3f931ad |
|
BLAKE2b-256 | 8d39de0d7e25c359667ba9b566d83d3bee1c7ec75ee1ae466447f6d976fbe2ac |
Close
Hashes for django_pydantic_field-0.0.8-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c6b2d42aa862e24c7ecce2ad70d017ffc82501cada1b78ea7f10f1a8a507c5b7 |
|
MD5 | 55868292170bc9764c0231c7b6cc6f2d |
|
BLAKE2b-256 | a065973310e97ad9fb689b97b099509913a293563be5a2ceee481db445c8bd5e |