skillfed

django-cte

Common Table Expressions (CTE) for Django

django-cte v3.0.0 1.1M downloads/30d#4,440 on PyPI428
Permissive license BSD-3-Clause Active released

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-cte

uv

uv add django-cte

poetry

poetry add django-cte

Installing 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

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

Tags

django common table expressionsdjango cte queriesdjango recursive queriesdjango hierarchical datadjango orm advanced queriesdjango with clausecte django orm
django-ormsql-ctehierarchical-queries

More Python Modules packages