django-sql-utils
Improved API for aggregating using Subquery
What it is and what it does
django-sql-utils wraps Django's queryset annotation API to generate more efficient SQL for common aggregation patterns. Instead of using Django's default JOIN-based aggregations, it provides subquery-based alternatives like SubqueryCount, SubqueryMin, SubqueryMax, SubquerySum, and SubqueryAvg that often perform better on large datasets. It also simplifies the syntax for annotating querysets with existence checks via a streamlined Exists API.
The package targets developers working with Django who need to optimize query performance or find Django's native aggregation syntax verbose. It depends on django and sqlparse as runtime dependencies and requires Python >= 3.8. The codebase has been dormant since 2023-09-28, so compatibility with the latest Django releases is not guaranteed without verification.
Use it for:
- Annotate a Parent queryset with a count of related Child objects using a subquery instead of a JOIN for better performance.
- Add a boolean annotation to check whether a parent has any children, using a simpler API than Django's native Exists.
- Use SubqueryAggregate with PostgreSQL aggregates to collect related field values without writing complex subquery syntax.
- Replace verbose Django subquery and OuterRef patterns with cleaner, more maintainable annotation calls.
- Optimize queries that would otherwise use GROUP BY and JOIN, which can be slower than subquery-based aggregation.
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
Provides simplified APIs for Django querysets to generate optimized SQL, particularly for subquery-based aggregations and existence checks that are more performant than Django's default JOIN-based approaches.
Yes, if you are actively using Django and need to optimize aggregation queries. The package solves a real performance problem and has a clean API. However, verify compatibility with your Django version first—the package has been dormant since 2023-09-28 and may not work with very recent Django releases. No known security vulnerabilities.
Install
django-sql-utils on PyPI
pip
pip install django-sql-utilsuv
uv add django-sql-utilspoetry
poetry add django-sql-utilsInstalling django-sql-utils
Before you install
Low friction: pure Python wheel with only django and sqlparse as runtime dependencies. Dormant since last commit on 2023-09-28; repository is not archived and has 143 stars, but no recent maintenance activity.
License in practice
MIT license (permissive); you can use, modify, and distribute this package freely with minimal restrictions.
Quickstart
pip install django-sql-utils
from django_sql_utils import SubqueryCount
Parent.objects.annotate(child_count=SubqueryCount('child'))
Requires Django and Python >= 3.8; the package is dormant and may not be compatible with very recent Django versions.
Verify before relying
- Compatibility with Django versions released after 2023-09-28 is unclear given the dormant maintenance status.
- Whether sqlparse is used directly by the package or only as a transitive dependency of django.
- The exact module path for importing SubqueryCount and other utilities from the package.
Package facts
| License | not declared (permissive) |
| Python support | supports the current Python release (>=3.8) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | 2 — django, sqlparse |
| Maintenance | dormant — 1,051 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 183,905/month — #10,053 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: django_sql_utils-0.7.0-py3-none-any.whl
Tags
More Database packages
psycopg2-binary is a PostgreSQL database…
copyleft · top 1,000 on PyPI
redisPython client library for connecting to and…
permissive · top 1,000 on PyPI
ydbYDB Python SDK is the official client library…
permissive · top 1,000 on PyPI
snowflake-connector-pythonConnects Python applications to Snowflake data…
permissive · top 1,000 on PyPI
sqlparsesqlparse tokenizes SQL text into a tree of…
permissive · top 1,000 on PyPI
dbt-adaptersProvides base adapter protocols and shared…
permissive · top 1,000 on PyPI
django-tree-queriesQuery hierarchical tree structures in Django…
permissive · top 15,000 on PyPI
django-cacheopsAutomatic and manual ORM query caching for…
permissive · top 5,000 on PyPI
django-query-builderBuilds and executes complex SQL queries…
permissive · top 15,000 on PyPI
django-mock-queriesMocks Django QuerySet operations in memory for…
permissive · top 15,000 on PyPI
django-chunkatorIterates over large Django QuerySets in…
permissive · top 15,000 on PyPI
django-cteAdds Common Table Expressions (CTE) support to…
permissive · top 5,000 on PyPI
django-pg-returningExtends Django's ORM to capture and return rows…
permissive · top 15,000 on PyPI
gw-dsl-parserConverts graphic-walker workflow definitions…
permissive · top 15,000 on PyPI
django-pg-bulk-updateExecutes bulk update and bulk create operations…
permissive · top 15,000 on PyPI
django-mysqlDjango-MySQL extends Django's ORM with MySQL…
permissive · top 15,000 on PyPI