From 63794da99d8fa2fa8ae7731073d97893ab130a06 Mon Sep 17 00:00:00 2001 From: Jock Tanner Date: Wed, 26 Feb 2020 23:51:27 +1000 Subject: [PATCH 1/2] Fix descending choice building in OrderingFilter. --- django_filters/filters.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/django_filters/filters.py b/django_filters/filters.py index 7331bfd1d..40958253d 100644 --- a/django_filters/filters.py +++ b/django_filters/filters.py @@ -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 ] From 2fb594059a77f890424ac00130e9197f23b62b2b Mon Sep 17 00:00:00 2001 From: Jock Tanner Date: Wed, 26 Feb 2020 23:52:22 +1000 Subject: [PATCH 2/2] Modify test case. --- tests/test_filters.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/tests/test_filters.py b/tests/test_filters.py index d2061f19f..22233abc0 100644 --- a/tests/test_filters.py +++ b/tests/test_filters.py @@ -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):