Skip to content

Commit 83f9fa7

Browse files
committed
Fixes jieter#854
I don't think this is perfect but I haven't dug in deep enough to know what would be.
1 parent 9d7941f commit 83f9fa7

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

django_tables2/columns/base.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,12 @@ def order(self, queryset, is_descending):
397397
returns:
398398
Tuple (QuerySet, boolean)
399399
"""
400-
return (queryset, False)
400+
from django.db.models import F, OrderBy
401+
402+
if self.order_by or self.accessor: # TODO: self.orderable
403+
return queryset, OrderBy(F(self.order_by or self.accessor), descending=is_descending)
404+
405+
return queryset, None
401406

402407
@classmethod
403408
def from_field(cls, field, **kwargs):

django_tables2/data.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,8 @@ def order_by(self, aliases):
197197
columns ('-' indicates descending order) in order of
198198
significance with regard to data ordering.
199199
"""
200-
modified_any = False
201200
accessors = []
201+
orderables = tuple()
202202
for alias in aliases:
203203
bound_column = self.table.columns[OrderBy(alias).bare]
204204
# bound_column.order_by reflects the current ordering applied to
@@ -210,14 +210,15 @@ def order_by(self, aliases):
210210
accessors += bound_column.order_by
211211

212212
if bound_column:
213-
queryset, modified = bound_column.order(self.data, alias[0] == "-")
213+
# We have to pass queryset because ordering could be on an annotation that was
214+
# added for sorting
215+
self.data, ordering = bound_column.order(self.data, alias[0] == "-")
214216

215-
if modified:
216-
self.data = queryset
217-
modified_any = True
217+
orderables = orderables + (ordering,)
218218

219219
# custom ordering
220-
if modified_any:
220+
if orderables:
221+
self.data = self.data.order_by(*orderables)
221222
return
222223

223224
# Traditional ordering

0 commit comments

Comments
 (0)