django-cte
Common Table Expressions (CTE) for Django
Decision gist · record as of 2026-08-14
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.AI-flagged interpretation of the facts on this page — verify before relying
Before you install
- Requires Django 4.2 or later and Python 3.9 or later; database backend must support CTEs.
- 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 · maintenance · safety
BSD-3-Clause (permissive) — BSD-3-Clause is permissive; you can use this package in commercial and proprietary projects with minimal restrictions, provided you include the license notice.
last release 2026-02-05 (190 days) · last repo commit 2026-08-12 · 428 stars
0 known vulnerabilities (OSV.dev, 2026-08-14) · 1,052,054 downloads/mo, #4,440 on PyPI
Alternatives
Verify before relying
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(...)- 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.
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 on it.
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
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).
Requires Django 4.2 or later and Python 3.9 or later; database backend must support CTEs.
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(...)
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 packagedjango |
| 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 |
| Classifiers | Development Status :: 5 - Production/StableEnvironment :: Web EnvironmentFramework :: DjangoFramework :: Django :: 4Framework :: Django :: 4.2Framework :: Django :: 5Framework :: Django :: 5.1Framework :: Django :: 5.2Intended Audience :: DevelopersOperating System :: OS IndependentProgramming Language :: PythonProgramming Language :: Python :: 3Programming Language :: Python :: 3.10Programming Language :: Python :: 3.11Programming Language :: Python :: 3.12Programming Language :: Python :: 3.13Programming Language :: Python :: 3.14Programming Language :: Python :: 3.9Topic :: Software Development :: Libraries :: Python Modules |
Evidence: django_cte-3.0.0-py3-none-any.whl
Tags
Let your AI agent find packages like this
Example. Real query, live index.
You found this page by searching. An agent finds it by wishing: SkillFed indexes 14,416 PyPI packages by what they can do, searchable in plain language.
wish › “django common table expressions”
- django-cteAdds Common Table Expressions (CTE) support to Django's ORM, enabling…
- django-tree-queriesQuery hierarchical tree structures in Django models using recursive…
- scim2-filter-parserParses SCIM 2.0 filter query strings into abstract syntax trees and…
Give your agent the search over MCP, or paste the wish link into any chat.
More Python Modules packages
Converts domain names between Unicode and ASCII-compatible encoding (Punycode) according to IDNA 2008 and Unicode Technical Standard 46, with security validation and broader script coverage than the standard library.
Install it if you work with internationalized domain names, need to validate domains, or use HTTP clients that depend on it transitively.
Setuptools is a Python build backend and package management tool that handles building, distributing, and installing Python packages, including support for C/C++ extension modules.
PyYAML parses and emits YAML 1.1 data format, enabling serialization and deserialization of configuration files and Python objects to and from human-readable YAML text.
Pydantic validates Python data structures against type hints, coercing and checking input at runtime to ensure it matches a declared schema.
Provides reusable metadata objects for use with PEP-593 `typing.Annotated` to express common constraints like bounds, collection sizes, and predicates on types.
Install it if you use or build libraries that need to express type constraints in a standardized, inspectable way—or if you want to annotate your own types with…
Provides runtime tools to inspect and introspect Python type annotations, enabling programmatic examination of type hints at execution time.
See also django-tree-queries · django-query-builder · config · django-mysql · django-sql-utils · sqlalchemy-mixins · odata-query · django-pg-bulk-update · django-multitenant · djongo