Registry / web-framework / drf-pydantic

drf-pydantic

JSON →
library2.9.1pypypi✓ verified 84d ago

drf-pydantic is a Python library that enables seamless integration of Pydantic models with the Django REST Framework (DRF). It allows developers to use Pydantic for data validation and serialization within DRF serializers, leveraging Pydantic's robust schema definition and validation capabilities. The current version is 2.9.1, and it typically follows a release cadence tied to Django, DRF, or Pydantic updates, with minor releases for features and bug fixes.

pip install drf-pydantic
INSTALL
IMPORT
SIG · DRF-PYDANTIC
D
drf-pydantic
web-frameworkpythonv2.9.1
Install
5.8s avg
Import
Disk
84MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v2.9.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.910 runs
installs and imports cleanly · install 0.0s · import 0.000s · 83.9MB
glibc
py 3.103.910 runs
installs and imports cleanly · install 5.8s · import 0.000s · 84MB
84MB installed
● package 84MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

BaseModel
from drf_pydantic import BaseModel
from drf_pydantic.serializers import PydanticSerializer
DrfPydanticSerializer
from drf_pydantic import DrfPydanticSerializer
from drf_pydantic.serializers import PydanticSerializer

This quickstart demonstrates how to define a Pydantic model, create a `PydanticSerializer` for it, and then use it within a DRF `APIView` for both validating incoming data (POST) and serializing outgoing data (GET). The validated data from the serializer is a Pydantic model instance.

from pydantic import BaseModel, Field from typing import Optional from drf_pydantic.serializers import PydanticSerializer from rest_framework.views import APIView from rest_framework.response import Response from rest_framework import status # 1. Define your Pydantic model class ItemCreate(BaseModel): name: str = Field(..., max_length=100) description: Optional[str] = None price: float = Field(..., gt=0) # 2. Create a PydanticSerializer class ItemCreateSerializer(PydanticSerializer): class Meta: model = ItemCreate # 3. Use the serializer in a DRF View class ItemAPIView(APIView): def post(self, request): serializer = ItemCreateSerializer(data=request.data) serializer.is_valid(raise_exception=True) # Access validated data as a Pydantic model instance item_instance: ItemCreate = serializer.validated_data # In a real application, you would save `item_instance` to a database # or perform other business logic here. print(f"Received item: {item_instance.dict()}") return Response(item_instance.dict(), status=status.HTTP_201_CREATED) def get(self, request): # Example of serializing a Pydantic instance for output example_item = ItemCreate(name="Sample Widget", price=29.99) serializer = ItemCreateSerializer(instance=example_item) return Response(serializer.data)
Debug
Known issues
breakingVersion 2.x was a complete rewrite, introducing `PydanticSerializer` and `PydanticModelSerializer` as the primary components. Code written for 1.x (which used `PydanticField` and `PydanticMixin`) is not compatible with 2.x.
fix
Rewrite serializers using `PydanticSerializer` or `PydanticModelSerializer` and their `Meta` classes. Consult the official documentation for v2.x migration.
affects: <2.0.0
gotchaThere are two main serializers: `PydanticSerializer` for general Pydantic `BaseModel`s, and `PydanticModelSerializer` for Pydantic models specifically generated from Django models (often using `django-pydantic`). Using `PydanticModelSerializer` with a regular `BaseModel` not derived from a Django model will lead to errors.
fix
Ensure you are using the correct serializer for your Pydantic model type. Use `PydanticSerializer` for standalone Pydantic models. Use `PydanticModelSerializer` only when your Pydantic model is designed to map directly to a Django ORM model.
affects: >=2.0.0
gotchaWhen using `PydanticSerializer` or `PydanticModelSerializer`, you must explicitly define `class Meta: model = YourPydanticModel` within your serializer. Forgetting this or misconfiguring it will prevent the serializer from functioning correctly.
fix
Always include `class Meta: model = YourPydanticModel` where `YourPydanticModel` is your Pydantic `BaseModel` or Django-derived Pydantic model.
affects: >=2.0.0
Upgrade
Version history
2.9.1latest on PyPI · released Dec 14, 2025
Audit
Dependencies
djangorestframeworkrequiredCore dependency for Django REST Framework integration.
pydanticrequiredCore dependency for Pydantic model validation and serialization.
Agent activity
8 hits · last 30 days
node
6
Resources
drf-pydantic — pip install drf-pydantic · libregistry