Clarified the values accepted by ModelAdmin.fields.

This commit is contained in:
Adam Johnson 2018-08-03 18:17:23 +02:00 committed by Tim Graham
parent 058d33f3ed
commit 45086c294d
1 changed files with 10 additions and 12 deletions

View File

@ -308,9 +308,9 @@ subclass::
For more complex layout needs, see the :attr:`~ModelAdmin.fieldsets` option. For more complex layout needs, see the :attr:`~ModelAdmin.fieldsets` option.
The ``fields`` option, unlike :attr:`~ModelAdmin.list_display`, may only The ``fields`` option accepts the same types of values as
contain names of fields on the model or the form specified by :attr:`~ModelAdmin.list_display`, except that callables aren't accepted.
:attr:`~ModelAdmin.form`. It may contain callables only if they are listed Names of model and model admin methods will only be used if they're listed
in :attr:`~ModelAdmin.readonly_fields`. in :attr:`~ModelAdmin.readonly_fields`.
To display multiple fields on the same line, wrap those fields in their own To display multiple fields on the same line, wrap those fields in their own
@ -550,15 +550,14 @@ subclass::
If you don't set ``list_display``, the admin site will display a single If you don't set ``list_display``, the admin site will display a single
column that displays the ``__str__()`` representation of each object. column that displays the ``__str__()`` representation of each object.
You have four possible values that can be used in ``list_display``: There are four types of values that can be used in ``list_display``:
* A field of the model. For example:: * The name of a model field. For example::
class PersonAdmin(admin.ModelAdmin): class PersonAdmin(admin.ModelAdmin):
list_display = ('first_name', 'last_name') list_display = ('first_name', 'last_name')
* A callable that accepts one parameter for the model instance. For * A callable that accepts one argument, the model instance. For example::
example::
def upper_case_name(obj): def upper_case_name(obj):
return ("%s %s" % (obj.first_name, obj.last_name)).upper() return ("%s %s" % (obj.first_name, obj.last_name)).upper()
@ -567,8 +566,8 @@ subclass::
class PersonAdmin(admin.ModelAdmin): class PersonAdmin(admin.ModelAdmin):
list_display = (upper_case_name,) list_display = (upper_case_name,)
* A string representing an attribute on the ``ModelAdmin``. This * A string representing a ``ModelAdmin`` method that accepts one argument,
behaves same as the callable. For example:: the model instance. For example::
class PersonAdmin(admin.ModelAdmin): class PersonAdmin(admin.ModelAdmin):
list_display = ('upper_case_name',) list_display = ('upper_case_name',)
@ -577,9 +576,8 @@ subclass::
return ("%s %s" % (obj.first_name, obj.last_name)).upper() return ("%s %s" % (obj.first_name, obj.last_name)).upper()
upper_case_name.short_description = 'Name' upper_case_name.short_description = 'Name'
* A string representing an attribute on the model. This behaves almost * A string representing a model attribute or method (without any required
the same as the callable, but ``self`` in this context is the model arguments). For example::
instance. Here's a full model example::
from django.contrib import admin from django.contrib import admin
from django.db import models from django.db import models