Skip to main content

Test 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.5.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.5.0
File Size Uploaded
drf_pydantic-0.5.0.tar.gz 6.1 kB Details

Built distribution (wheel)

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

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

This release

0.5.0 This release

2 release files

0.4.1

2 release files

0.4.0

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