skillfed

django-sql-utils

Improved API for aggregating using Subquery

django-sql-utils v0.7.0 183.9K downloads/30d#10,053 on PyPI143
Permissive license DORMANT released

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

uv

uv add django-sql-utils

poetry

poetry add django-sql-utils

Installing 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

License :: OSI Approved :: MIT LicenseOperating System :: OS IndependentProgramming Language :: Python :: 3

Tags

django subquery aggregationdjango queryset optimizationdjango sql generationdjango count subquerydjango exists annotationdjango query performancedjango aggregate subquery
django-ormquery-optimizationsql-generation

More Database packages

psycopg2-binary

psycopg2-binary is a PostgreSQL database…

copyleft · top 1,000 on PyPI

redis

Python client library for connecting to and…

permissive · top 1,000 on PyPI

ydb

YDB Python SDK is the official client library…

permissive · top 1,000 on PyPI

snowflake-connector-python

Connects Python applications to Snowflake data…

permissive · top 1,000 on PyPI

sqlparse

sqlparse tokenizes SQL text into a tree of…

permissive · top 1,000 on PyPI

dbt-adapters

Provides base adapter protocols and shared…

permissive · top 1,000 on PyPI

django-tree-queries

Query hierarchical tree structures in Django…

permissive · top 15,000 on PyPI

django-cacheops

Automatic and manual ORM query caching for…

permissive · top 5,000 on PyPI

django-query-builder

Builds and executes complex SQL queries…

permissive · top 15,000 on PyPI

django-mock-queries

Mocks Django QuerySet operations in memory for…

permissive · top 15,000 on PyPI

django-chunkator

Iterates over large Django QuerySets in…

permissive · top 15,000 on PyPI

django-cte

Adds Common Table Expressions (CTE) support to…

permissive · top 5,000 on PyPI

django-pg-returning

Extends Django's ORM to capture and return rows…

permissive · top 15,000 on PyPI

gw-dsl-parser

Converts graphic-walker workflow definitions…

permissive · top 15,000 on PyPI

django-pg-bulk-update

Executes bulk update and bulk create operations…

permissive · top 15,000 on PyPI

django-mysql

Django-MySQL extends Django's ORM with MySQL…

permissive · top 15,000 on PyPI