Browse Source

PaginatedQuery: set a default for _sort_column

Previously, _sort_column wasn't set by default and needed to be set by
the query, but some of the other methods would fail if it hadn't been
set. This just defaults it to use the created_time column as the default
sort, which should always be present on models being queried by this
class (for now, at least).
merge-requests/22/head
Deimos 6 years ago
parent
commit
f95a504ca4
  1. 4
      tildes/tildes/models/pagination.py

4
tildes/tildes/models/pagination.py

@ -22,7 +22,8 @@ class PaginatedQuery(ModelQuery):
super().__init__(model_cls, request) super().__init__(model_cls, request)
self._sort_column: Optional[Column] = None
# default to sorting by created_time descending (newest first)
self._sort_column = model_cls.created_time
self.sort_desc = True self.sort_desc = True
self.after_id: Optional[int] = None self.after_id: Optional[int] = None
@ -135,7 +136,6 @@ class PaginatedQuery(ModelQuery):
"""Finalize the query before execution.""" """Finalize the query before execution."""
query = super()._finalize() query = super()._finalize()
if self._sort_column:
# if the query is reversed, we need to sort in the opposite dir # if the query is reversed, we need to sort in the opposite dir
# (basically self.sort_desc XOR self.is_reversed) # (basically self.sort_desc XOR self.is_reversed)
desc = self.sort_desc desc = self.sort_desc

Loading…
Cancel
Save