Skip to content

Commit

Permalink
Fixes jieter#854
Browse files Browse the repository at this point in the history
I don't think this is perfect but I haven't dug in deep enough to know
what would be.
  • Loading branch information
boatcoder committed Jul 12, 2022
1 parent 9d7941f commit 83f9fa7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
7 changes: 6 additions & 1 deletion django_tables2/columns/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,12 @@ def order(self, queryset, is_descending):
returns:
Tuple (QuerySet, boolean)
"""
return (queryset, False)
from django.db.models import F, OrderBy

if self.order_by or self.accessor: # TODO: self.orderable
return queryset, OrderBy(F(self.order_by or self.accessor), descending=is_descending)

return queryset, None

@classmethod
def from_field(cls, field, **kwargs):
Expand Down
13 changes: 7 additions & 6 deletions django_tables2/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,8 @@ def order_by(self, aliases):
columns ('-' indicates descending order) in order of
significance with regard to data ordering.
"""
modified_any = False
accessors = []
orderables = tuple()
for alias in aliases:
bound_column = self.table.columns[OrderBy(alias).bare]
# bound_column.order_by reflects the current ordering applied to
Expand All @@ -210,14 +210,15 @@ def order_by(self, aliases):
accessors += bound_column.order_by

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

if modified:
self.data = queryset
modified_any = True
orderables = orderables + (ordering,)

# custom ordering
if modified_any:
if orderables:
self.data = self.data.order_by(*orderables)
return

# Traditional ordering
Expand Down

0 comments on commit 83f9fa7

Please # to comment.