django-cte
Common Table Expressions (CTE) for Django
What it is and what it does
django-cte extends Django's ORM with support for Common Table Expressions, a SQL feature that allows you to define temporary named result sets within a query. This is essential for recursive queries (like traversing organizational hierarchies or comment threads) and complex multi-step aggregations that the standard Django QuerySet API cannot express directly.
The package works by providing a With() helper to define CTE clauses and integrating them into Django querysets. It sits on top of django as a single runtime dependency and maintains compatibility with modern Django versions (4.2, 5.1, 5.2) and Python 3.9 and later. The project is actively maintained, has no known security vulnerabilities, and is used by a moderate but steady audience (top 5000 on PyPI).
Use it for:
- Build recursive queries to traverse tree structures (org charts, file hierarchies, comment threads) without multiple database round-trips.
- Implement hierarchical permission or role checks by walking up or down a parent-child relationship in a single query.
- Aggregate data across multiple levels of a hierarchy in one pass.
- Deduplicate or filter complex result sets by defining intermediate named result sets within a single query.
- Optimize queries that would otherwise require multiple separate database calls or expensive in-memory processing.
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
Adds Common Table Expressions (CTE) support to Django's ORM, enabling recursive and hierarchical queries that the standard Django QuerySet API does not provide.
Yes, if you need recursive or hierarchical queries in Django. The package is stable (Production/Stable status), actively maintained, has no security issues, and adds a single lightweight dependency. Install it only if your application actually requires CTE functionality; standard Django querysets are sufficient for most CRUD operations.
Install
django-cte on PyPI
pip
pip install django-cteuv
uv add django-ctepoetry
poetry add django-cteInstalling django-cte
Before you install
Low friction install with a single runtime dependency on django. The package is actively maintained with a recent release (190 days ago), repo is not archived, and supports current Python versions (3.9–3.14) and Django versions (4.2, 5.1, 5.2).
License in practice
BSD-3-Clause is permissive; you can use this package in commercial and proprietary projects with minimal restrictions, provided you include the license notice.
Quickstart
pip install django-cte
from django_cte import With
from django.db import models
# Define a CTE and integrate it into a queryset
cte = With(
models.QuerySet.filter(...),
name='cte_name'
)
results = models.QuerySet.with_cte(cte).filter(...)
Requires Django 4.2 or later and Python 3.9 or later; database backend must support CTEs.
Verify before relying
- Whether CTE support is database-agnostic or limited to specific backends (e.g., PostgreSQL, MySQL, SQLite).
- Performance characteristics when working with large recursive result sets or deeply nested hierarchies.
- Specific usage patterns and API design beyond what the description excerpt shows.
Package facts
| License | BSD-3-Clause (permissive) |
| Python support | supports the current Python release (>=3.9) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | 1 — django |
| Maintenance | actively maintained — 190 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 1,052,054/month — #4,440 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: django_cte-3.0.0-py3-none-any.whl
Tags
More Python Modules packages
Converts domain names between Unicode and…
permissive · top 100 on PyPI
setuptoolsSetuptools is a Python build backend and…
permissive · top 100 on PyPI
PyYAMLPyYAML parses and emits YAML 1.1 data format,…
permissive · top 100 on PyPI
pydanticPydantic validates Python data structures…
permissive · top 100 on PyPI
annotated-typesProvides reusable metadata objects for use with…
permissive · top 100 on PyPI
typing-inspectionProvides runtime tools to inspect and…
permissive · top 100 on PyPI
django-tree-queriesQuery hierarchical tree structures in Django…
permissive · top 15,000 on PyPI
django-query-builderBuilds and executes complex SQL queries…
permissive · top 15,000 on PyPI
configProvides hierarchical configuration management…
permissive · top 5,000 on PyPI
django-mysqlDjango-MySQL extends Django's ORM with MySQL…
permissive · top 15,000 on PyPI
django-sql-utilsProvides simplified APIs for Django querysets…
permissive · top 15,000 on PyPI
sqlalchemy-mixinsProvides Active Record-style mixins for…
permissive · top 15,000 on PyPI
odata-queryParses OData v4 filter strings and transpiles…
permissive · top 15,000 on PyPI
django-pg-bulk-updateExecutes bulk update and bulk create operations…
permissive · top 15,000 on PyPI
django-multitenantAdds multi-tenant database support to Django by…
permissive · top 15,000 on PyPI
djongoDjongo translates Django ORM queries into…
permissive · top 15,000 on PyPI