Registry / database / django-sql-utils

django-sql-utils

JSON →
library0.7.0pypypiunverified

Improved API for aggregating using Subquery in Django. Provides SubqueryAggregate, SubqueryCount, SubquerySum, etc. Current version 0.7.0, supports Django 1.11 to 3.0+. Maintenance frequency: irregular, last release 2020.

pip install django-sql-utils
INSTALL
IMPORT
SIG · DJANGO-SQL-UTILS
D
django-sql-utils
databasepythonv0.7.0
Install
3.3s avg
Import
Disk
65MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.7.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.000s · 66.3MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 3.3s · import 0.000s · 67MB
65MB installed
● package 65MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

SubqueryAggregate
from sql_util import SubqueryAggregate
from sql_util import SubqueryAggregate
SubqueryCount
from sql_util import SubqueryCount
SubquerySum
from sql_util import SubquerySum

Replace 'myapp.models' with your actual app/models. The SubqueryAggregate family works like Django's aggregates but generates subqueries.

from sql_util import SubqueryCount, SubquerySum from myapp.models import Author, Book # Annotate each author with count of books qs = Author.objects.annotate( book_count=SubqueryCount('book') ) # Annotate each author with total price of books where price > 10 from django.db.models import Q qs = Author.objects.annotate( expensive_total=SubquerySum('book__price', filter=Q(book__price__gt=10)) ) # Use with updates (Django 2.0+) Author.objects.update( book_count=SubqueryCount('book') )
Debug
Known issues
gotchaAggregating over ForeignKey IDs fails if the FK column name differs from the related model's default id column. Use the related field name (e.g., 'author') not the column name.
fix
Use SubqueryCount('author') instead of SubqueryCount('author_id') or SubqueryCount('author__id').
affects: >=0.4.1
breakingRequires Python >=3.8 and Django >=1.11. No official support for Django 4.0+ or Python 3.11+. May break with newer Django versions.
fix
Test thoroughly with your Django version. Consider switching to Django's built-in Subquery() and annotate() if using Django 3.0+.
affects: 0.7.0
deprecatedThe package is no longer actively maintained. Last release 2020. For Django 3.2+ you may not need this library; Django's Subquery expressions now cover most use cases.
fix
Migrate to Django's annotate() with Subquery() and OuterRef().
affects: all
gotchaWhen using update(), SubqueryAggregate may raise an error if related_name and related_query_name differ on ForeignKey. Fixed in 0.7.0 but still fragile.
fix
Upgrade to 0.7.0 or ensure related_name and related_query_name match.
affects: 0.6.x
Upgrade
Version history
0.7.0latest on PyPI · released Sep 28, 2023
Audit
Dependencies

No dependency data recorded yet.

Agent activity
14 hits · last 30 days
node
14
Resources
django-sql-utils — pip install django-sql-utils · libregistry