Skip to content

Commit

Permalink
[1.2.X] Fixed #11058 - list_display_links doesn't allow callables not…
Browse files Browse the repository at this point in the history
… defined in the model

Thanks to dvine for the report and julien for the patch.

Backport of [15619] from trunk.

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.2.X@15621 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
spookylukey committed Feb 21, 2011
1 parent 25c217b commit 31d0f2f
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 10 deletions.
3 changes: 1 addition & 2 deletions django/contrib/admin/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,8 @@ def validate(cls, model):
if hasattr(cls, 'list_display_links'):
check_isseq(cls, 'list_display_links', cls.list_display_links)
for idx, field in enumerate(cls.list_display_links):
fetch_attr(cls, model, opts, 'list_display_links[%d]' % idx, field)
if field not in cls.list_display:
raise ImproperlyConfigured("'%s.list_display_links[%d]'"
raise ImproperlyConfigured("'%s.list_display_links[%d]' "
"refers to '%s' which is not defined in 'list_display'."
% (cls.__name__, idx, field))

Expand Down
8 changes: 4 additions & 4 deletions docs/ref/contrib/admin/index.txt
Original file line number Diff line number Diff line change
Expand Up @@ -408,12 +408,12 @@ subclass::

By default, the change list page will link the first column -- the first
field specified in ``list_display`` -- to the change page for each item.
But``list_display_links`` lets you change which columns are linked. Set
``list_display_links`` to a list or tuple of field names (in the same
But ``list_display_links`` lets you change which columns are linked. Set
``list_display_links`` to a list or tuple of fields (in the same
format as ``list_display``) to link.

``list_display_links`` can specify one or many field names. As long as the
field names appear in ``list_display``, Django doesn't care how many (or
``list_display_links`` can specify one or many fields. As long as the
fields appear in ``list_display``, Django doesn't care how many (or
how few) fields are linked. The only requirement is: If you want to use
``list_display_links``, you must define ``list_display``.

Expand Down
5 changes: 4 additions & 1 deletion tests/regressiontests/modeladmin/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Band(models.Model):
name = models.CharField(max_length=100)
bio = models.TextField()
sign_date = models.DateField()

def __unicode__(self):
return self.name

Expand All @@ -32,5 +32,8 @@ class ValidationTestModel(models.Model):
pub_date = models.DateTimeField()
band = models.ForeignKey(Band)

def decade_published_in(self):
return self.pub_date.strftime('%Y')[:3] + "0's"

class ValidationTestInlineModel(models.Model):
parent = models.ForeignKey(ValidationTestModel)
18 changes: 15 additions & 3 deletions tests/regressiontests/modeladmin/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -778,8 +778,13 @@ class ValidationTestModelAdmin(ModelAdmin):
ValidationTestModel,
)

def a_callable(obj):
pass

class ValidationTestModelAdmin(ModelAdmin):
list_display = ('name',)
def a_method(self, obj):
pass
list_display = ('name', 'decade_published_in', 'a_method', a_callable)

validate(ValidationTestModelAdmin, ValidationTestModel)

Expand All @@ -801,7 +806,7 @@ class ValidationTestModelAdmin(ModelAdmin):

self.assertRaisesErrorWithMessage(
ImproperlyConfigured,
"'ValidationTestModelAdmin.list_display_links[0]' refers to 'non_existent_field' that is neither a field, method or property of model 'ValidationTestModel'.",
"'ValidationTestModelAdmin.list_display_links[0]' refers to 'non_existent_field' which is not defined in 'list_display'.",
validate,
ValidationTestModelAdmin,
ValidationTestModel,
Expand All @@ -812,15 +817,22 @@ class ValidationTestModelAdmin(ModelAdmin):

self.assertRaisesErrorWithMessage(
ImproperlyConfigured,
"'ValidationTestModelAdmin.list_display_links[0]'refers to 'name' which is not defined in 'list_display'.",
"'ValidationTestModelAdmin.list_display_links[0]' refers to 'name' which is not defined in 'list_display'.",
validate,
ValidationTestModelAdmin,
ValidationTestModel,
)

def a_callable(obj):
pass

class ValidationTestModelAdmin(ModelAdmin):
list_display = ('name',)
list_display_links = ('name',)
def a_method(self, obj):
pass
list_display = ('name', 'decade_published_in', 'a_method', a_callable)
list_display_links = ('name', 'decade_published_in', 'a_method', a_callable)

validate(ValidationTestModelAdmin, ValidationTestModel)

Expand Down

0 comments on commit 31d0f2f

Please sign in to comment.