Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Something to fix ordering when using order_FOO and normal field names. #855

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, orderable = bound_column.order(self.data, alias[0] == "-")

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

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

# Traditional ordering
Expand Down