summaryrefslogtreecommitdiff
path: root/docs
diff options
context:
space:
mode:
authorBrian Rosner <brosner@gmail.com>2008-08-14 20:12:19 +0000
committerBrian Rosner <brosner@gmail.com>2008-08-14 20:12:19 +0000
commitb2ec6473c09bcad88194cfb52252c44ed7e960aa (patch)
treeee849bf6c234dfaa810bfb5f47ba4356c0aaa54a /docs
parent9e423b51e325c9226e2f744bfa52336a626bf63a (diff)
downloaddjango-b2ec6473c09bcad88194cfb52252c44ed7e960aa.tar.gz
Fixed #7503 -- Allow callables in list_display. This also does a lookup on the ModelAdmin for the method if the value is a string before looking on the model. Refs #8054. Thanks qmanic and Daniel Pope for tickets and patches.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@8352 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'docs')
-rw-r--r--docs/admin.txt75
1 files changed, 51 insertions, 24 deletions
diff --git a/docs/admin.txt b/docs/admin.txt
index dba28ecbdf..03c0154331 100644
--- a/docs/admin.txt
+++ b/docs/admin.txt
@@ -201,25 +201,36 @@ Example::
If you don't set ``list_display``, the admin site will display a single column
that displays the ``__unicode__()`` representation of each object.
-A few special cases to note about ``list_display``:
-
- * If the field is a ``ForeignKey``, Django will display the
- ``__unicode__()`` of the related object.
-
- * ``ManyToManyField`` fields aren't supported, because that would entail
- executing a separate SQL statement for each row in the table. If you
- want to do this nonetheless, give your model a custom method, and add
- that method's name to ``list_display``. (See below for more on custom
- methods in ``list_display``.)
-
- * If the field is a ``BooleanField`` or ``NullBooleanField``, Django will
- display a pretty "on" or "off" icon instead of ``True`` or ``False``.
-
- * If the string given is a method of the model, Django will call it and
- display the output. This method should have a ``short_description``
- function attribute, for use as the header for the field.
+You have four possible values that can be used in ``list_display``:
- Here's a full example model::
+ * A field of the model. For example::
+
+ class PersonAdmin(admin.ModelAdmin):
+ list_display = ('first_name', 'last_name')
+
+ * A callable that accepts one parameter for the model instance. For
+ example::
+
+ def upper_case_name(obj):
+ return "%s %s" % (obj.first_name, obj.last_name).upper()
+ upper_case_name.short_description = 'Name'
+
+ class PersonAdmin(admin.ModelAdmin):
+ list_display = (upper_case_name,)
+
+ * A string representating an attribute on the ``ModelAdmin``. This behaves
+ the same as the callable. For example::
+
+ class PersonAdmin(admin.ModelAdmin):
+ list_display = ('upper_case_name',)
+
+ def upper_case_name(self, obj):
+ return "%s %s" % (obj.first_name, obj.last_name).upper()
+ upper_case_name.short_description = 'Name'
+
+ * A string representating an attribute on the model. This behaves almost
+ the same as the callable, but ``self`` in this context is the model
+ instance. Here's a full model example::
class Person(models.Model):
name = models.CharField(max_length=50)
@@ -232,10 +243,25 @@ A few special cases to note about ``list_display``:
class PersonAdmin(admin.ModelAdmin):
list_display = ('name', 'decade_born_in')
- * If the string given is a method of the model, Django will HTML-escape the
- output by default. If you'd rather not escape the output of the method,
- give the method an ``allow_tags`` attribute whose value is ``True``.
+A few special cases to note about ``list_display``:
+
+ * If the field is a ``ForeignKey``, Django will display the
+ ``__unicode__()`` of the related object.
+
+ * ``ManyToManyField`` fields aren't supported, because that would entail
+ executing a separate SQL statement for each row in the table. If you
+ want to do this nonetheless, give your model a custom method, and add
+ that method's name to ``list_display``. (See below for more on custom
+ methods in ``list_display``.)
+
+ * If the field is a ``BooleanField`` or ``NullBooleanField``, Django will
+ display a pretty "on" or "off" icon instead of ``True`` or ``False``.
+ * If the string given is a method of the model, ``ModelAdmin`` or a
+ callable, Django will HTML-escape the output by default. If you'd rather
+ not escape the output of the method, give the method an ``allow_tags``
+ attribute whose value is ``True``.
+
Here's a full example model::
class Person(models.Model):
@@ -250,9 +276,10 @@ A few special cases to note about ``list_display``:
class PersonAdmin(admin.ModelAdmin):
list_display = ('first_name', 'last_name', 'colored_name')
- * If the string given is a method of the model that returns True or False
- Django will display a pretty "on" or "off" icon if you give the method a
- ``boolean`` attribute whose value is ``True``.
+ * If the string given is a method of the model, ``ModelAdmin`` or a
+ callable that returns True or False Django will display a pretty "on" or
+ "off" icon if you give the method a ``boolean`` attribute whose value is
+ ``True``.
Here's a full example model::