0
0
mirror of https://github.com/django/django.git synced 2024-12-01 15:42:04 +01:00

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
This commit is contained in:
Luke Plant 2011-06-01 23:17:40 +00:00
parent 2367089392
commit 2304ca4236
2 changed files with 20 additions and 1 deletions

View File

@ -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

View File

@ -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('<th scope="col">\s*Gallery\s*</th>', 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').