diff --git a/wagtail/admin/views/pages/listing.py b/wagtail/admin/views/pages/listing.py index 3a38b10acf..a589d68138 100644 --- a/wagtail/admin/views/pages/listing.py +++ b/wagtail/admin/views/pages/listing.py @@ -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)