0
0
mirror of https://github.com/wagtail/wagtail.git synced 2024-12-01 11:41:20 +01:00

Sort objects in descending order by updated_at and pk

This commit is contained in:
Sage Abdullah 2022-06-27 18:47:43 +07:00 committed by Karl Hobley
parent 0cae35496c
commit fd8d57a0ea
2 changed files with 6 additions and 6 deletions

View File

@ -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

View File

@ -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):