Skip to main content

CI/CD Status Test Coverage PyPI version

Use pydantic with Django REST framework

Introduction

Pydantic is a great Python library 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 and only the drf_serializer attribute is added on class creation.

Custom Base Models

You can also use it as a mixin with your existing pydantic models (no need to change your existing code 🥳):

from drf_pydantic import BaseModel as DRFBaseModel
from pydantic import BaseModel

class MyBaseModel(BaseModel):
  value: int

class MyModel(DRFBaseModel, MyBaseModel):
  name: str
  addresses: list[str]

⚠️ ATTENTION
Inheritance order is important: drf_pydantic.BaseModel must always go before the pydantic.BaseModel class.

Roadmap

  • Add ENUM support
  • Add option to create custom serializer for complex models

Metadata

Release files for drf-pydantic 0.2.1

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.2.1
File Size Uploaded
drf-pydantic-0.2.1.tar.gz 5.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for drf-pydantic 0.2.1
File Interpreter ABI Platform
drf_pydantic-0.2.1-py3-none-any.whl Python 3 none any Details

Total release size: 11.2 kB

Release files / drf-pydantic-0.2.1.tar.gz

Download URL drf-pydantic-0.2.1.tar.gz
Size 5.7 kB
Tags Source
SHA-256 checksum
How to use checksums
4e22211d79de0d82f6849497388eeaecd7b8f3372023233592f861ac7ffeeebc
BLAKE2b-256 checksum
How to use checksums
dd397e86dc4070fd13847719699e9fc7a79bc003f6f915cd9b9d01bea93ed144
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.2.0 CPython/3.9.13 Linux/5.15.0-1017-azure

Release files / drf_pydantic-0.2.1-py3-none-any.whl

Download URL drf_pydantic-0.2.1-py3-none-any.whl
Size 5.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f9f5b9f1a6c98500b13d2ffb76d8c5cf429bde17643b54a48e95e02e5b154a01
BLAKE2b-256 checksum
How to use checksums
3995a3740b467356f59ee306cfc73d051678d37b6086ebe7fd46cca59ca9e504
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.2.0 CPython/3.9.13 Linux/5.15.0-1017-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

0.4.0

2 release files

0.3.0

2 release files

This release

0.2.1 This release

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