--- id: django-cte version: "3.0.0" license: BSD-3-Clause license_treatment: permissive maintenance: active --- # django-cte — Common Table Expressions (CTE) for Django License: permissive · Maintenance: active · Downloads: 1.1M/mo ## 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 above — 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 pip install django-cte uv add django-cte 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_current - Install friction: low - Maintenance: active - Downloads: 1.1M/month (top 5,000 on PyPI) - Known vulnerabilities: none known ## Tags django common table expressions, django cte queries, django recursive queries, django hierarchical data, django orm advanced queries, django with clause, cte django orm, django-orm, sql-cte, hierarchical-queries [View on SkillFed](https://skillfed.io/packages/django-cte) · [View on PyPI](https://pypi.org/project/django-cte/)