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 support for custom field types (both for pydantic and DRF)
- Add option to create custom serializer for complex models
- Add support for constraints (max, min, regex, etc.)
Metadata
Release files for drf-pydantic 2.0.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-2.0.0.tar.gz | 8.7 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| drf_pydantic-2.0.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 17.0 kB
Release files / drf_pydantic-2.0.0.tar.gz
| Download URL | drf_pydantic-2.0.0.tar.gz |
|---|---|
| Size | 8.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a2e213f2bf4af342d04b8fab01492a72a7eebf72392237808d63a62829a554de
|
|
BLAKE2b-256 checksum How to use checksums |
2ac87bb499369596c4aad64842d1f1e036284769fc75fa491d7e8134a0586370
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.7.1 CPython/3.9.18 Linux/6.2.0-1018-azure
|
Release files / drf_pydantic-2.0.0-py3-none-any.whl
| Download URL | drf_pydantic-2.0.0-py3-none-any.whl |
|---|---|
| Size | 8.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
178183d843ac85c29c321c551c8d60e769bc371d0830a59d6f4f984be1cc3342
|
|
BLAKE2b-256 checksum How to use checksums |
16168a0a6c88027a912bf087cb4cf2ed4584794796d91956fb434f93108d1f52
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.7.1 CPython/3.9.18 Linux/6.2.0-1018-azure
|