From 2304ca423640a7b06390530bf61c879f9ff9e4ba Mon Sep 17 00:00:00 2001 From: Luke Plant Date: Wed, 1 Jun 2011 23:17:40 +0000 Subject: [PATCH] Fixed bug with `__str__` headers in admin changelist have a non-functioning sort URL git-svn-id: http://code.djangoproject.com/svn/django/trunk@16312 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/contrib/admin/util.py | 8 ++++++++ tests/regressiontests/admin_views/tests.py | 13 ++++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/django/contrib/admin/util.py b/django/contrib/admin/util.py index e98ec7b884..7204a12fc4 100644 --- a/django/contrib/admin/util.py +++ b/django/contrib/admin/util.py @@ -226,6 +226,12 @@ def lookup_field(name, obj, model_admin=None): def label_for_field(name, model, model_admin=None, return_attr=False): + """ + Returns a sensible label for a field name. The name can be a callable or the + name of an object attributes, as well as a genuine fields. If return_attr is + True, the resolved attribute (which could be a callable) is also returned. + This will be None if (and only if) the name refers to a field. + """ attr = None try: field = model._meta.get_field_by_name(name)[0] @@ -236,8 +242,10 @@ def label_for_field(name, model, model_admin=None, return_attr=False): except models.FieldDoesNotExist: if name == "__unicode__": label = force_unicode(model._meta.verbose_name) + attr = unicode elif name == "__str__": label = smart_str(model._meta.verbose_name) + attr = str else: if callable(name): attr = name diff --git a/tests/regressiontests/admin_views/tests.py b/tests/regressiontests/admin_views/tests.py index 54d89e4d75..cc5783cc86 100644 --- a/tests/regressiontests/admin_views/tests.py +++ b/tests/regressiontests/admin_views/tests.py @@ -32,7 +32,7 @@ from django.utils import unittest # local test models from models import (Article, BarAccount, CustomArticle, EmptyModel, - FooAccount, Gallery, ModelWithStringPrimaryKey, + FooAccount, Gallery, GalleryAdmin, ModelWithStringPrimaryKey, Person, Persona, Picture, Podcast, Section, Subscriber, Vodcast, Language, Collector, Widget, Grommet, DooHickey, FancyDoodad, Whatsit, Category, Post, Plot, FunkyTag, Chapter, Book, Promo, WorkHour, Employee, @@ -238,6 +238,17 @@ class AdminViewBasicTest(TestCase): "Results of sorting on ModelAdmin method are out of order." ) + def testChangeListSortColumnsDefault(self): + # Need a model that has a list_display with '__str__' as only item. + # Sanity check for assumption made in following test. + self.assertEqual(list(GalleryAdmin.list_display), ['__str__']) + # A header corresponding to '__str__' should not be in an anchor + # for sorting. + g = Gallery.objects.create(name='gallery1') + response = self.client.get('/test_admin/%s/admin_views/gallery/' % self.urlbit, {}) + m = re.search('\s*Gallery\s*', response.content) + self.assertTrue(m is not None) + def testLimitedFilter(self): """Ensure admin changelist filters do not contain objects excluded via limit_choices_to. This also tests relation-spanning filters (e.g. 'color__value').