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.5.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.5.0.tar.gz | 6.1 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| drf_pydantic-0.5.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 12.8 kB
Release files / drf_pydantic-0.5.0.tar.gz
| Download URL | drf_pydantic-0.5.0.tar.gz |
|---|---|
| Size | 6.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
4ee36ef549a0d0d22e9c52c287572128f66e0d56f7619e3844363d49193af5d7
|
|
BLAKE2b-256 checksum How to use checksums |
57ba8f894cc1a4817accc53d669473de62f2e8eeef58a7036a8e1de705890421
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.5.1 CPython/3.9.17 Linux/5.15.0-1041-azure
|
Release files / drf_pydantic-0.5.0-py3-none-any.whl
| Download URL | drf_pydantic-0.5.0-py3-none-any.whl |
|---|---|
| Size | 6.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
06683f88ee37f862297822cbf0bcc9c9a7271ae9c5a47f230d66cd10126e7d84
|
|
BLAKE2b-256 checksum How to use checksums |
b4b146e1d31da1fe03d2de6dbd1e9678824a8ddea49835c2f08436eb9ad9cb8c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.5.1 CPython/3.9.17 Linux/5.15.0-1041-azure
|