Home
Django 4.2+
Pydantic v2
Python 3.10+
django-pydantic-client
Skip the serializer. Validate Django requests
with Pydantic models — one line, zero boilerplate.
$
pip install django-pydantic-client
One-line validation
Pass the Django request directly to your Pydantic model. No manual parsing, no request.data, no boilerplate.
Automatic 422 errors
Validation failures return structured JSON error responses automatically. No try/except needed in your views.
Zero configuration
Add django_pydantic to INSTALLED_APPS. The middleware registers itself. Nothing else required.
Full Pydantic v2 power
All validators, Field() constraints, model_config, computed fields, and custom types work as-is.
Merges all data sources
GET params, POST form data, JSON body, and URL path parameters — merged transparently into one validated object.
Works with CBVs too
Fully compatible with Django class-based views and Django REST Framework. Drop it into any existing project.
The idea in 30 seconds¶
Without django-pydantic-client
def signup(request):
import json
try:
body = json.loads(request.body)
except json.JSONDecodeError:
return JsonResponse({"error": "bad json"}, status=400)
username = body.get("username")
email = body.get("email")
age = body.get("age")
if not username:
return JsonResponse({"error": "username required"}, status=400)
if not email or "@" not in email:
return JsonResponse({"error": "bad email"}, status=400)
if not isinstance(age, int) or age < 0:
return JsonResponse({"error": "bad age"}, status=400)
# finally do something useful
With django-pydantic-client
# schema.py
from pydantic import EmailStr, Field
from django_pydantic import RequestModel
class SignupSchema(RequestModel):
username: str
email: EmailStr
age: int = Field(ge=0)
# views.py
def signup(request):
data = SignupSchema(request)
# data.username, data.email, data.age
# all validated, typed, ready to use
Quick example¶
from django.http import JsonResponse
from .schema import SignupSchema
def signup(request):
data = SignupSchema(request) # (1)!
# User.objects.create(...)
return JsonResponse({"username": data.username})
- Accepts JSON body, POST form data, or GET query params. Returns HTTP 422 with structured errors on failure — automatically.