0
0
mirror of https://github.com/wagtail/wagtail.git synced 2024-11-29 01:22:07 +01:00

Use Django's built-in for ordering nulls first/last, instead of a custom annotation

This commit is contained in:
Matt Westcott 2024-01-22 14:44:28 +00:00 committed by Matt Westcott
parent 97f80bfe20
commit 8d32c8cb8d

View File

@ -1,6 +1,6 @@
from django.conf import settings
from django.contrib.auth import get_user_model
from django.db.models import Count
from django.db.models import F
from django.forms import CheckboxSelectMultiple, RadioSelect
from django.shortcuts import get_object_or_404, redirect
from django.urls import reverse
@ -288,17 +288,15 @@ class BaseIndexView(generic.IndexView):
elif self.ordering == "latest_revision_created_at":
# order by oldest revision first.
# Special case NULL entries - these should go at the top of the list.
# Do this by annotating with Count('latest_revision_created_at'),
# which returns 0 for these
queryset = queryset.annotate(
null_position=Count("latest_revision_created_at")
).order_by("null_position", "latest_revision_created_at")
queryset = queryset.order_by(
F("latest_revision_created_at").asc(nulls_first=True)
)
elif self.ordering == "-latest_revision_created_at":
# order by oldest revision first.
# Special case NULL entries - these should go at the end of the list.
queryset = queryset.annotate(
null_position=Count("latest_revision_created_at")
).order_by("-null_position", "-latest_revision_created_at")
queryset = queryset.order_by(
F("latest_revision_created_at").desc(nulls_last=True)
)
else:
queryset = super().order_queryset(queryset)