Use pydantic with Django REST framework
Introduction
Pydantic is a Python library used to perform data serialization and validation.
Django REST framework is a framework built on top of Django which allows writing REST APIs.
If like me you develop DRF APIs and you like pydantic , drf-pydantic is for you 😍.
Installation
pip install drf-pydantic
Usage
General
Use drf_pydantic.BaseModel instead of pydantic.BaseModel when creating your
models:
from drf_pydantic import BaseModel
class MyModel(BaseModel):
name: str
addresses: list[str]
Whenever you need a DRF serializer you can get it from the model like this:
MyModel.drf_serializer
ℹ️ INFO
Models created usingdrf_pydanticare fully idenditcal to those created bypydantic. The only change is the addition of thedrf_serializerattribute during class creation (not instance).
Existing Models
If you have an existing code base and you would like to use the drf_serializer
attribute to only specific models, then great news 🥳 - you can easily extend
your existign pydantic models by adding drf_pydantic.BaseModel to the list
of parent classes.
Your existing pydantic models:
from pydantic import BaseModel
class Pet(BaseModel):
name: str
class Dog(Pet):
breed: str
Update your Dog model and get serializer via the drf_serializer:
from drf_pydantic import BaseModel as DRFBaseModel
from pydantic import BaseModel
class Pet(BaseModel):
name: str
class Dog(DRFBaseModel, Pet):
breed: str
Dog.drf_serializer
⚠️ ATTENTION
Inheritance order is important:drf_pydantic.BaseModelmust always go before thepydantic.BaseModelclass.
Nested Models
If you have nested models and you want to generate serializer only from one of them,
you don't have to update all models - only update the model you need, drf_pydantic
will generate serializers for all normal nested pydantic models for free 🐱👤.
from drf_pydantic import BaseModel as DRFBaseModel
from pydantic import BaseModel
class Apartment(BaseModel):
floor: int
tenant: str
class Building(BaseModel):
address: str
aparments: list[Apartment]
class Block(DRFBaseModel):
buildings: list[Buildind]
Block.drf_serializer
Roadmap
- Add
ENUMsupport - Add option to create custom serializer for complex models
- Add support for constraints (max, min, regex, etc.)
Metadata
Release files for drf-pydantic 0.4.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| drf_pydantic-0.4.0.tar.gz | 5.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| drf_pydantic-0.4.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 11.4 kB
Release files / drf_pydantic-0.4.0.tar.gz
| Download URL | drf_pydantic-0.4.0.tar.gz |
|---|---|
| Size | 5.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
09e0cc004300b0c80c3c823c0533aa4a6ec56867bb3b7c02c9a5774d499bdeab
|
|
BLAKE2b-256 checksum How to use checksums |
90eaa76cc922d8eca58f38efbd75fb30180e478ff42827d0538643e7495f3f70
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.4.2 CPython/3.10.10 Linux/5.15.0-1035-azure
|
Release files / drf_pydantic-0.4.0-py3-none-any.whl
| Download URL | drf_pydantic-0.4.0-py3-none-any.whl |
|---|---|
| Size | 5.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
10d9e346420620a9be7252c7949f88e74d2b19d3ddf37f061e4f2093e5cec07d
|
|
BLAKE2b-256 checksum How to use checksums |
e5af4c04fd9308ad25acd40556453fc2edd4dac7b1bc2a8f55694d573bb7d3e4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.4.2 CPython/3.10.10 Linux/5.15.0-1035-azure
|