Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed bug with `__str__` headers in admin changelist have a non-funct…

…ioning sort URL

git-svn-id: http://code.djangoproject.com/svn/django/trunk@16312 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 2304ca423640a7b06390530bf61c879f9ff9e4ba 1 parent 2367089
Luke Plant spookylukey authored
8 django/contrib/admin/util.py
@@ -226,6 +226,12 @@ def lookup_field(name, obj, model_admin=None):
226 226
227 227
228 228 def label_for_field(name, model, model_admin=None, return_attr=False):
  229 + """
  230 + Returns a sensible label for a field name. The name can be a callable or the
  231 + name of an object attributes, as well as a genuine fields. If return_attr is
  232 + True, the resolved attribute (which could be a callable) is also returned.
  233 + This will be None if (and only if) the name refers to a field.
  234 + """
229 235 attr = None
230 236 try:
231 237 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):
236 242 except models.FieldDoesNotExist:
237 243 if name == "__unicode__":
238 244 label = force_unicode(model._meta.verbose_name)
  245 + attr = unicode
239 246 elif name == "__str__":
240 247 label = smart_str(model._meta.verbose_name)
  248 + attr = str
241 249 else:
242 250 if callable(name):
243 251 attr = name
13 tests/regressiontests/admin_views/tests.py
@@ -32,7 +32,7 @@
32 32
33 33 # local test models
34 34 from models import (Article, BarAccount, CustomArticle, EmptyModel,
35   - FooAccount, Gallery, ModelWithStringPrimaryKey,
  35 + FooAccount, Gallery, GalleryAdmin, ModelWithStringPrimaryKey,
36 36 Person, Persona, Picture, Podcast, Section, Subscriber, Vodcast,
37 37 Language, Collector, Widget, Grommet, DooHickey, FancyDoodad, Whatsit,
38 38 Category, Post, Plot, FunkyTag, Chapter, Book, Promo, WorkHour, Employee,
@@ -238,6 +238,17 @@ def testChangeListSortingModelAdmin(self):
238 238 "Results of sorting on ModelAdmin method are out of order."
239 239 )
240 240
  241 + def testChangeListSortColumnsDefault(self):
  242 + # Need a model that has a list_display with '__str__' as only item.
  243 + # Sanity check for assumption made in following test.
  244 + self.assertEqual(list(GalleryAdmin.list_display), ['__str__'])
  245 + # A header corresponding to '__str__' should not be in an anchor
  246 + # for sorting.
  247 + g = Gallery.objects.create(name='gallery1')
  248 + response = self.client.get('/test_admin/%s/admin_views/gallery/' % self.urlbit, {})
  249 + m = re.search('<th scope="col">\s*Gallery\s*</th>', response.content)
  250 + self.assertTrue(m is not None)
  251 +
241 252 def testLimitedFilter(self):
242 253 """Ensure admin changelist filters do not contain objects excluded via limit_choices_to.
243 254 This also tests relation-spanning filters (e.g. 'color__value').

0 comments on commit 2304ca4

Please sign in to comment.
Something went wrong with that request. Please try again.