{"id":21163,"library":"django-sql-utils","title":"django-sql-utils","description":"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.","status":"maintenance","version":"0.7.0","language":"python","source_language":"en","source_url":"https://github.com/martsberger/django-sql-utils","tags":["django","subquery","aggregate","orm"],"install":[{"cmd":"pip install django-sql-utils","lang":"bash","label":"PyPI"}],"dependencies":[],"imports":[{"note":"Correct import uses sql_util, not django_sql_utils.","wrong":"from django_sql_utils import SubqueryAggregate","symbol":"SubqueryAggregate","correct":"from sql_util import SubqueryAggregate"},{"note":"SubqueryCount is directly importable from sql_util.","wrong":"from sql_util.aggregates import SubqueryCount","symbol":"SubqueryCount","correct":"from sql_util import SubqueryCount"},{"note":"SubquerySum is directly importable from sql_util.","wrong":"from sql_util.aggregates import SubquerySum","symbol":"SubquerySum","correct":"from sql_util import SubquerySum"},{"note":"Exists is a custom subclass of django.db.models.expressions.Subquery.","wrong":null,"symbol":"Exists","correct":"from sql_util import Exists"}],"quickstart":{"code":"from sql_util import SubqueryCount, SubquerySum\nfrom myapp.models import Author, Book\n\n# Annotate each author with count of books\nqs = Author.objects.annotate(\n    book_count=SubqueryCount('book')\n)\n\n# Annotate each author with total price of books where price > 10\nfrom django.db.models import Q\nqs = Author.objects.annotate(\n    expensive_total=SubquerySum('book__price', filter=Q(book__price__gt=10))\n)\n\n# Use with updates (Django 2.0+)\nAuthor.objects.update(\n    book_count=SubqueryCount('book')\n)","lang":"python","description":"Replace 'myapp.models' with your actual app/models. The SubqueryAggregate family works like Django's aggregates but generates subqueries."},"warnings":[{"fix":"Use SubqueryCount('author') instead of SubqueryCount('author_id') or SubqueryCount('author__id').","message":"Aggregating 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.","severity":"gotcha","affected_versions":">=0.4.1"},{"fix":"Test thoroughly with your Django version. Consider switching to Django's built-in Subquery() and annotate() if using Django 3.0+.","message":"Requires Python >=3.8 and Django >=1.11. No official support for Django 4.0+ or Python 3.11+. May break with newer Django versions.","severity":"breaking","affected_versions":"0.7.0"},{"fix":"Migrate to Django's annotate() with Subquery() and OuterRef().","message":"The 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.","severity":"deprecated","affected_versions":"all"},{"fix":"Upgrade to 0.7.0 or ensure related_name and related_query_name match.","message":"When 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.","severity":"gotcha","affected_versions":"0.6.x"}],"env_vars":null,"last_verified":"2026-04-27T00:00:00.000Z","next_check":"2026-07-26T00:00:00.000Z","problems":[{"fix":"Run: pip install django-sql-utils","cause":"Package not installed or wrong import name.","error":"ImportError: No module named 'sql_util'"},{"fix":"Use the field name without '_id', e.g., SubqueryCount('author') instead of SubqueryCount('author_id')","cause":"Using the database column name (e.g., 'author_id') instead of the relation name (e.g., 'author') in SubqueryAggregate.","error":"django.core.exceptions.FieldError: Cannot resolve keyword 'author_id' into field. Choices are: ..."},{"fix":"Upgrade Django to at least 1.11. If using Django 3.2+, consider using Django's built-in Subquery.","cause":"Using an older version of Django (<1.11) or incompatible Django version.","error":"AttributeError: 'SubqueryAggregate' object has no attribute 'resolve_expression'"}],"ecosystem":"pypi","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}