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

Fix descending choices building for OrderingFilter #1176

Closed
wants to merge 2 commits into from
Closed
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
5 changes: 3 additions & 2 deletions django_filters/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -737,12 +737,13 @@ def normalize_fields(cls, fields):
])

def build_choices(self, fields, labels):
fields = {param: field for field, param in fields.items()}
ascending = [
(param, labels.get(field, _(pretty_name(param))))
for field, param in fields.items()
for param, field in fields.items()
]
descending = [
('-%s' % param, labels.get('-%s' % param, self.descending_fmt % label))
('-%s' % param, labels.get('-%s' % fields[param], self.descending_fmt % label))
for param, label in ascending
rpkilby marked this conversation as resolved.
Show resolved Hide resolved
]

Expand Down
12 changes: 7 additions & 5 deletions tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -1508,17 +1508,19 @@ def test_field_labels(self):

def test_field_labels_descending(self):
f = OrderingFilter(
fields=['username'],
fields=(('a', 'c'), ('b', 'd')),
field_labels={
'username': 'BLABLA',
'-username': 'XYZXYZ',
'-a': 'BLABLA',
'-b': 'XYZXYZ',
}
)

self.assertEqual(list(f.field.choices), [
('', '---------'),
('username', 'BLABLA'),
('-username', 'XYZXYZ'),
('c', 'C'),
('-c', 'BLABLA'),
('d', 'D'),
('-d', 'XYZXYZ'),
])

def test_normalize_fields(self):
Expand Down