diff options
| author | Klemens Mantzos <klemens@fetzig.at> | 2014-01-23 03:13:59 +0100 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2014-02-14 19:53:44 -0500 |
| commit | f683cb90bea2afbe0ef4c011acd4ab590c37410d (patch) | |
| tree | fdb2b7528378f60979fd5e32a27ae80594626962 | |
| parent | 0242134d32aa99a54442211ed05576b7061866d1 (diff) | |
Fixed #21924 -- Added the ability to specify a reverse order for admin_order_field.
Thanks Klemens Mantzos for the report and initial patch.
| -rw-r--r-- | django/contrib/admin/views/main.py | 6 | ||||
| -rw-r--r-- | docs/ref/contrib/admin/index.txt | 8 | ||||
| -rw-r--r-- | docs/releases/1.7.txt | 4 | ||||
| -rw-r--r-- | tests/admin_views/admin.py | 3 | ||||
| -rw-r--r-- | tests/admin_views/models.py | 5 | ||||
| -rw-r--r-- | tests/admin_views/tests.py | 21 |
6 files changed, 44 insertions, 3 deletions
diff --git a/django/contrib/admin/views/main.py b/django/contrib/admin/views/main.py index dccab55de4..c22225927c 100644 --- a/django/contrib/admin/views/main.py +++ b/django/contrib/admin/views/main.py @@ -293,7 +293,11 @@ class ChangeList(six.with_metaclass(RenameChangeListMethods)): order_field = self.get_ordering_field(field_name) if not order_field: continue # No 'admin_order_field', skip it - ordering.append(pfx + order_field) + # reverse order if order_field has already "-" as prefix + if order_field.startswith('-') and pfx == "-": + ordering.append(order_field[1:]) + else: + ordering.append(pfx + order_field) except (IndexError, ValueError): continue # Invalid ordering specified, skip it. diff --git a/docs/ref/contrib/admin/index.txt b/docs/ref/contrib/admin/index.txt index 28de1aa612..e44ecc8e55 100644 --- a/docs/ref/contrib/admin/index.txt +++ b/docs/ref/contrib/admin/index.txt @@ -664,6 +664,14 @@ subclass:: The above will tell Django to order by the ``first_name`` field when trying to sort by ``colored_first_name`` in the admin. + .. versionadded:: 1.7 + + To indicate descending order with ``admin_model_field`` you can use a + hyphen prefix on the field name. Using the above example, this would + look like:: + + colored_first_name.admin_order_field = '-first_name' + * Elements of ``list_display`` can also be properties. Please note however, that due to the way properties work in Python, setting ``short_description`` on a property is only possible when using the diff --git a/docs/releases/1.7.txt b/docs/releases/1.7.txt index d2f1c39ec3..8e9ebe72a1 100644 --- a/docs/releases/1.7.txt +++ b/docs/releases/1.7.txt @@ -292,6 +292,10 @@ Minor features <django.contrib.admin.ModelAdmin.view_on_site>` to control whether or not to display the "View on site" link. +* You can specify a descending ordering for a :attr:`ModelAdmin.list_display + <django.contrib.admin.ModelAdmin.list_display>` value by prefixing the + ``admin_order_field`` value with a hyphen. + :mod:`django.contrib.auth` ^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/tests/admin_views/admin.py b/tests/admin_views/admin.py index e17942e831..ae625b6a53 100644 --- a/tests/admin_views/admin.py +++ b/tests/admin_views/admin.py @@ -76,7 +76,8 @@ class ChapterXtra1Admin(admin.ModelAdmin): class ArticleAdmin(admin.ModelAdmin): - list_display = ('content', 'date', callable_year, 'model_year', 'modeladmin_year') + list_display = ('content', 'date', callable_year, 'model_year', + 'modeladmin_year', 'model_year_reversed') list_filter = ('date', 'section') view_on_site = False fieldsets = ( diff --git a/tests/admin_views/models.py b/tests/admin_views/models.py index 4cc58f5e88..c729f6b90a 100644 --- a/tests/admin_views/models.py +++ b/tests/admin_views/models.py @@ -41,6 +41,11 @@ class Article(models.Model): model_year.admin_order_field = 'date' model_year.short_description = '' + def model_year_reversed(self): + return self.date.year + model_year_reversed.admin_order_field = '-date' + model_year_reversed.short_description = '' + @python_2_unicode_compatible class Book(models.Model): diff --git a/tests/admin_views/tests.py b/tests/admin_views/tests.py index f5c90bc1d0..41ac47bafd 100644 --- a/tests/admin_views/tests.py +++ b/tests/admin_views/tests.py @@ -260,7 +260,7 @@ class AdminViewBasicTest(AdminViewBasicTestCase): def testChangeListSortingModelAdmin(self): """ Ensure we can sort on a list_display field that is a ModelAdmin method - (colunn 4 is 'modeladmin_year' in ArticleAdmin) + (column 4 is 'modeladmin_year' in ArticleAdmin) """ response = self.client.get('/test_admin/%s/admin_views/article/' % self.urlbit, {'o': '4'}) self.assertContentBefore(response, 'Oldest content', 'Middle content', @@ -268,6 +268,25 @@ class AdminViewBasicTest(AdminViewBasicTestCase): self.assertContentBefore(response, 'Middle content', 'Newest content', "Results of sorting on ModelAdmin method are out of order.") + def testChangeListSortingModelAdminReverse(self): + """ + Ensure we can sort on a list_display field that is a ModelAdmin + method in reverse order (i.e. admin_order_field uses the '-' prefix) + (column 6 is 'model_year_reverse' in ArticleAdmin) + """ + response = self.client.get('/test_admin/%s/admin_views/article/' % self.urlbit, {'o': '6'}) + self.assertContentBefore(response, '2009', '2008', + "Results of sorting on ModelAdmin method are out of order.") + self.assertContentBefore(response, '2008', '2000', + "Results of sorting on ModelAdmin method are out of order.") + # Let's make sure the ordering is right and that we don't get a + # FieldError when we change to descending order + response = self.client.get('/test_admin/%s/admin_views/article/' % self.urlbit, {'o': '-6'}) + self.assertContentBefore(response, '2000', '2008', + "Results of sorting on ModelAdmin method are out of order.") + self.assertContentBefore(response, '2008', '2009', + "Results of sorting on ModelAdmin method are out of order.") + def testChangeListSortingMultiple(self): p1 = Person.objects.create(name="Chris", gender=1, alive=True) p2 = Person.objects.create(name="Chris", gender=2, alive=True) |
