Skip to main content

CI/CD Status Test Coverage PyPI version

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 using drf_pydantic are fully idenditcal to those created by pydantic. The only change is the addition of the drf_serializer attribute 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.BaseModel must always go before the pydantic.BaseModel class.

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 ENUM support
  • 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)

Source distribution for drf-pydantic 0.4.0
File Size Uploaded
drf_pydantic-0.4.0.tar.gz 5.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for drf-pydantic 0.4.0
File Interpreter ABI Platform
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

Release history Release notifications | RSS feed

2.9.2

2 release files

2.9.1

2 release files

2.9.0

2 release files

2.8.0

2 release files

2.7.1

2 release files

2.7.0

2 release files

2.6.1

2 release files

2.6.0

2 release files

2.5.1

2 release files

2.5.0

2 release files

2.4.0

2 release files

2.3.0

2 release files

2.2.0

2 release files

2.1.0

2 release files

2.0.0

2 release files

1.0.0

2 release files

0.5.0

2 release files

0.4.1

2 release files

This release

0.4.0 This release

2 release files

0.3.0

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page