Only try to order by _updated_at if column exists

pull/10862/head
Matt Westcott 2023-08-08 00:35:09 +01:00 zatwierdzone przez Matt Westcott
rodzic cbeb9a2512
commit e40b0b1fd0
1 zmienionych plików z 12 dodań i 4 usunięć

Wyświetl plik

@ -219,7 +219,12 @@ class IndexView(
if self.locale:
queryset = queryset.filter(locale=self.locale)
queryset = self._annotate_queryset_updated_at(queryset)
has_updated_at_column = any(
getattr(column, "accessor", None) == "_updated_at"
for column in self.columns
)
if has_updated_at_column:
queryset = self._annotate_queryset_updated_at(queryset)
ordering = self.get_ordering()
if ordering:
@ -236,9 +241,12 @@ class IndexView(
# 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(
models.F("_updated_at").desc(nulls_last=True), "-pk"
)
if has_updated_at_column:
queryset = queryset.order_by(
models.F("_updated_at").desc(nulls_last=True), "-pk"
)
else:
queryset = queryset.order_by("-pk")
return queryset