From fd8d57a0ea28e8f11b93f73645b98d7aa1d7649a Mon Sep 17 00:00:00 2001 From: Sage Abdullah Date: Mon, 27 Jun 2022 18:47:43 +0700 Subject: [PATCH] Sort objects in descending order by updated_at and pk --- wagtail/admin/views/generic/models.py | 6 +++--- wagtail/snippets/tests/test_snippets.py | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/wagtail/admin/views/generic/models.py b/wagtail/admin/views/generic/models.py index 8d93b33875..59e638a1bc 100644 --- a/wagtail/admin/views/generic/models.py +++ b/wagtail/admin/views/generic/models.py @@ -160,10 +160,10 @@ class IndexView( ) queryset = queryset.annotate(_updated_at=models.Subquery(latest_log)) - # Preserve the model-level ordering if specified, but fall back on PK if not - # (to ensure pagination is consistent) + # Preserve the model-level ordering if specified, but fall back on + # updated_at and PK if not (to ensure pagination is consistent) if not queryset.ordered: - queryset = queryset.order_by("pk") + queryset = queryset.order_by("-_updated_at", "-pk") return queryset diff --git a/wagtail/snippets/tests/test_snippets.py b/wagtail/snippets/tests/test_snippets.py index 8a59c64475..67bf118c43 100644 --- a/wagtail/snippets/tests/test_snippets.py +++ b/wagtail/snippets/tests/test_snippets.py @@ -121,13 +121,13 @@ class TestSnippetListView(TestCase, WagtailTestUtils): def test_ordering(self): """ - Listing should be ordered by PK if no ordering has been set on the model + Listing should be ordered descending by PK if no ordering has been set on the model """ - for i in range(10, 0, -1): + for i in range(1, 11): Advert.objects.create(pk=i, text="advert %d" % i) response = self.get() self.assertEqual(response.status_code, 200) - self.assertEqual(response.context["items"][0].text, "advert 1") + self.assertEqual(response.context["items"][0].text, "advert 10") def test_simple_pagination(self):