/
Art86
/
DefectDojo-DevSecOps
Обзор
Документация
Войти
/
Art86
/
DefectDojo-DevSecOps
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
dojo/query_utils.py
14 строк
766 B
valentijnscholten
Fix finding counts showing as 1 due to subquery ordering bug (#14242)
05 фев 2026, 01:53
05 фев 2026, 01:53
eb98c2d
Код
Авторство
О чём код?
from django.db.models import Count, IntegerField, Subquery from django.db.models.query import QuerySet def build_count_subquery(model_qs: QuerySet, group_field: str) -> Subquery: """Return a Subquery that yields one aggregated count per `group_field`.""" # Important: slicing (`[:1]`) on an unordered queryset makes Django add an implicit `ORDER BY <pk>`. # With aggregation, Django then includes that pk in the GROUP BY, which collapses counts to 1. # Ordering by `group_field` avoids that and keeps the GROUP BY stable. model_qs = model_qs.order_by() return Subquery( model_qs.values(group_field).annotate(c=Count("pk")).order_by(group_field).values("c")[:1], # one row per group_field output_field=IntegerField(), )