Django for AI apps: how Khoj, Paperless-ngx, Plane, Baserow, PostHog and authentik use Python's web framework in 2026
What Django is
Django was created in 2003 at the Lawrence Journal-World newspaper in Kansas and released as open source in 2005. It follows a "batteries included" approach. A new Django project already has:
| Component | What it does |
|---|---|
| ORM and migrations | Define models in Python; Django creates and changes tables |
| Admin | A full data management interface generated from models |
| Auth | Users, groups, permissions, sessions and password hashing |
| Forms and validation | Server-side input checking |
| Security defaults | Protection against CSRF, SQL injection, XSS and clickjacking |
| Async support | Async views, middleware and ORM methods under ASGI |
The Django Software Foundation, a non-profit, maintains it. Django REST Framework and Django Ninja are the common choices for building APIs on top.
Which directory projects use Django
| Project | What it is | How Django is used |
|---|---|---|
| Khoj | Personal AI assistant that searches your notes and documents | Django back end with FastAPI routes for the assistant |
| Paperless-ngx | Scans, OCRs and organizes documents with automatic tagging | Django app with Celery workers for OCR and classification |
| Plane | Open-source project tracker, an alternative to Jira and Linear | Django REST API with a Next.js front end |
| Baserow | No-code database, an Airtable alternative with AI fields | Django back end with a Nuxt front end |
| PostHog | Product analytics, session replay, feature flags and more | Django app with ClickHouse for event data |
| authentik | Identity provider with SSO, OAuth2, SAML and LDAP | Django core with Go outposts |
These are large, long-lived codebases, which shows how Django scales when a team keeps its structure clean.
Recent releases and security
| Date | Release | Notes |
|---|---|---|
| August 4, 2026 | 6.0.8 | Fixed a high-severity spatial lookup issue that could allow file writes or request forgery, plus moderate and low issues |
| 2026 | 6.1.x | Current feature release; 6.1.2 is the latest patch |
| April 2027 | 6.2 LTS (planned) | Next long-term support release |
The August fixes rejected unsafe values in GeoDjango spatial lookups, limited nesting depth in geometry collections and validated URLField links before rendering. If you use GeoDjango or accept user-supplied geometry, upgrade promptly. Django publishes security releases on a predictable schedule with pre-notification to registered users, and supports each feature release until shortly after the next one; LTS releases get security fixes for about three years.
Adding AI features to a Django app
Calling models
Put model calls in a service module rather than in views, so you can change providers, add retries and test with fakes. For requests users wait on, use async views and an async client, or stream the response. For slow or batch work such as summarizing uploaded documents, use a task queue.
Background jobs
| Option | Notes |
|---|---|
| Celery | The long-standing choice; used by Paperless-ngx and PostHog; needs Redis or RabbitMQ |
| Django's tasks framework | Built-in background task interface added in Django 6.0, with pluggable backends |
| Dramatiq, Huey, RQ | Lighter alternatives |
| Temporal or Hatchet | Durable workflows for long multi-step agent jobs |
Redis is the most common broker; see the Redis stack page.
Retrieval and embeddings
With PostgreSQL and pgvector, add a VectorField to a model, generate embeddings in a background task when the record changes, and query with CosineDistance. Because vectors live in the same database as your other data, you can filter by owner, permissions and dates in one ORM query. For larger collections, a dedicated vector database such as Qdrant can sit alongside. See the PostgreSQL stack page.
Streaming responses
Under ASGI, an async view can return a StreamingHttpResponse that yields chunks from the model's stream as server-sent events. Run Django with Uvicorn, Daphne or Granian for async support, and make sure your reverse proxy does not buffer the response.
Admin as an AI back office
The Django admin is useful for AI apps: review flagged conversations, edit prompt templates stored in the database, inspect evaluation results and manage users' usage limits, all without building custom screens. Add custom admin actions such as "re-run classification" for selected records.
Example: an internal document assistant
A team building an internal Q&A tool on Django might use:
- A
Documentmodel with file uploads stored in S3-compatible storage. - A Celery task that extracts text, splits it into chunks and stores embeddings in a
Chunkmodel with a pgvector field. - Django's permission system to record which teams may read each document.
- An async view that embeds the question, retrieves the top chunks the user may see, and streams an answer with citations.
- Admin screens to review low-rated answers and re-index documents.
Paperless-ngx follows a similar shape for document classification, and Khoj for personal search.
Django compared with other Python frameworks
| Framework | Best for |
|---|---|
| Django | Full applications with database, auth and admin |
| FastAPI | Async APIs, model serving, microservices |
| Flask | Small apps and prototypes with free choice of components |
| Litestar | Async APIs with more built-in structure than FastAPI |
| Reflex | Full-stack apps written entirely in Python |
Deployment
- Run under Gunicorn for sync workloads or Uvicorn workers for async; use ASGI if you stream model output.
- Serve static files with WhiteNoise or a CDN.
- Use PostgreSQL in production, with connection pooling through PgBouncer or Django's built-in pooling for psycopg.
- Run migrations as a separate deploy step.
- Keep secrets, including model API keys, in environment variables or a secret manager, never in settings files committed to Git.
- Containerize with Docker; see the Docker stack page.
Security tips for AI features
Model output is untrusted input. Escape it in templates, which Django does by default, and be careful with mark_safe or rendering Markdown to HTML; sanitize with a library such as nh3. Never pass model output into raw SQL, eval or shell commands. Rate-limit endpoints that call paid models per user, and log prompts and responses with retention limits that match your privacy policy.
Upgrading Django
Django deprecates features two releases before removing them, and prints deprecation warnings in the meantime. Run your test suite with warnings enabled (python -Wd manage.py test) before each upgrade, fix the warnings, then move one feature release at a time. Third-party packages are usually the slowest part; check that Django REST Framework, Celery integrations and any admin plugins support the target version before you start. Projects on LTS releases can jump from one LTS to the next, such as 5.2 to 6.2, but still benefit from testing against the intermediate releases.
Related: Python stack, FastAPI stack, PostgreSQL stack, Redis stack, PostHog stack, Ory stack, Stack index.
Frequently asked questions
Is Django good for AI apps?
Yes, especially when the app needs users, permissions, an admin interface and a relational data model alongside AI features. Because it is Python, it can import model SDKs, LangChain, LlamaIndex or local model libraries directly. For a small API that only wraps a model, FastAPI is lighter.
Can Django stream model responses?
Yes. StreamingHttpResponse can send tokens as they arrive, and under ASGI with an async view it streams without tying up a worker thread. Django Channels adds WebSockets for two-way real-time features.
Should I use Django or FastAPI?
Django for full applications with a database, admin, auth and server-rendered pages or a large API. FastAPI for focused, async-first APIs and model-serving endpoints. Many teams use both: Django for the main app and FastAPI for an inference or retrieval service.
Which Django version should I use?
New projects can use Django 6.1. Projects that prefer fewer upgrades should stay on the current long-term support release, Django 5.2, until Django 6.2 LTS arrives in April 2027. Always apply security patch releases promptly.
How do I store embeddings in Django?
Use PostgreSQL with the pgvector extension and the pgvector-python package, which adds a VectorField and distance functions to the Django ORM. You can then filter by regular fields and sort by vector similarity in one query.