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 used to write REST APIs.
If you develop DRF APIs and rely on pydantic for data validation/(de)serialization ,
then 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]
MyModel.drf_serializer would be equvalent to the following DRF Serializer class:
class MyModelSerializer:
name = CharField(allow_null=False, required=True)
addresses = ListField(
allow_empty=True,
allow_null=False,
child=CharField(allow_null=False),
required=True,
)
Whenever you need a DRF serializer you can get it from the model like this:
my_value = MyModel.drf_serializer(data={"name": "Van", addresses: ["Gym"]})
my_value.is_valid(raise_exception=True)
ℹ️ INFO
Models created usingdrf_pydanticare fully idenditcal to those created bypydantic. The only change is the addition of thedrf_serializerattribute.
Existing Models
If you have an existing code base and you would like to add the drf_serializer
attribute only to some of your models, then I have great news 🥳 - you can easily
extend your existing pydantic models by adding drf_pydantic.BaseModel to the list
of parent classes of the model you want to extend.
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
Metadata
Release files for drf-pydantic 2.1.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.1.0.tar.gz | 9.1 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| drf_pydantic-2.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 17.8 kB
Release files / drf_pydantic-2.1.0.tar.gz
| Download URL | drf_pydantic-2.1.0.tar.gz |
|---|---|
| Size | 9.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
94adad2228c929ec86c8dd993f5291213b61d0b214aa4ee6faa36ccbbe38ba2f
|
|
BLAKE2b-256 checksum How to use checksums |
25b4c466aa29a7ba0554ff495e4e62ca52038a46644c37e2b4706112cbb998af
|
| 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.1.0-py3-none-any.whl
| Download URL | drf_pydantic-2.1.0-py3-none-any.whl |
|---|---|
| Size | 8.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
0bf7dba54bb26d14a4b39d5823a47ffbd2fee46bb7b24d21a7c674bcfd52ac9a
|
|
BLAKE2b-256 checksum How to use checksums |
b50b7f12a17a6754a278a96bc5e5637aaf114240251a65d43b2ab1be5a200a77
|
| 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
|