Skip to content

Commit 73ace77

Browse files
committed
fix: update nested plugin to use Max aggregation to prevent duplication of rows
1 parent 30a0dbe commit 73ace77

2 files changed

Lines changed: 8 additions & 4 deletions

File tree

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "django-smartbase-admin"
3-
version = "1.1.13b2"
3+
version = "1.1.13b3"
44
description = ""
55
authors = ["SmartBase <info@smartbase.sk>"]
66
readme = "README.md"

src/django_smartbase_admin/plugins/nested.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646

4747
from django.contrib.postgres.aggregates import ArrayAgg
4848
from django.core.exceptions import FieldDoesNotExist, ImproperlyConfigured
49-
from django.db.models import F, Q, OuterRef, Subquery
49+
from django.db.models import F, Max, Q, OuterRef, Subquery
5050
from django.db.models.functions import Coalesce
5151

5252
from django_smartbase_admin.plugins.base import SBAdminPlugin
@@ -395,8 +395,12 @@ def _build_grouped_qs(
395395
sort_parent_qs = action.get_data_queryset(
396396
visible_fields=[visible_field] if visible_field else []
397397
)
398-
sort_annotations[alias] = Subquery(
399-
sort_parent_qs.filter(id=OuterRef("parent_real_id")).values(field)[:1]
398+
sort_annotations[alias] = Max(
399+
Subquery(
400+
sort_parent_qs.filter(id=OuterRef("parent_real_id")).values(field)[
401+
:1
402+
]
403+
)
400404
)
401405
new_order.append(f"-{alias}" if desc else alias)
402406
return grouped.annotate(**sort_annotations).order_by(*new_order)

0 commit comments

Comments
 (0)