1
0
mirror of https://github.com/django/django.git synced 2025-07-19 09:09:13 +00:00

[1.0.X]: Fixed #9946 -- Removed redundant mention of needing to define list_display.

Backport of r10237 from trunk.


git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.0.X@10238 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Gary Wilson Jr 2009-03-30 23:33:08 +00:00
parent 9bbf94112f
commit 9d808c14a5

View File

@ -64,9 +64,9 @@ Let's take a look at a very simple example of the ``ModelAdmin``::
from django.contrib import admin from django.contrib import admin
from myproject.myapp.models import Author from myproject.myapp.models import Author
admin.site.register(Author) admin.site.register(Author)
``ModelAdmin`` Options ``ModelAdmin`` Options
---------------------- ----------------------
@ -138,13 +138,13 @@ The ``field_options`` dictionary can have the following keys:
* ``fields`` * ``fields``
A tuple of field names to display in this fieldset. This key is A tuple of field names to display in this fieldset. This key is
required. required.
Example:: Example::
{ {
'fields': ('first_name', 'last_name', 'address', 'city', 'state'), 'fields': ('first_name', 'last_name', 'address', 'city', 'state'),
} }
To display multiple fields on the same line, wrap those fields in To display multiple fields on the same line, wrap those fields in
their own tuple. In this example, the ``first_name`` and ``last_name`` their own tuple. In this example, the ``first_name`` and ``last_name``
fields will display on the same line:: fields will display on the same line::
@ -155,9 +155,9 @@ The ``field_options`` dictionary can have the following keys:
* ``classes`` * ``classes``
A list containing extra CSS classes to apply to the fieldset. A list containing extra CSS classes to apply to the fieldset.
Example:: Example::
{ {
'classes': ['wide', 'extrapretty'], 'classes': ['wide', 'extrapretty'],
} }
@ -213,10 +213,10 @@ For example, let's consider the following model::
If you want a form for the ``Author`` model that includes only the ``name`` If you want a form for the ``Author`` model that includes only the ``name``
and ``title`` fields, you would specify ``fields`` or ``exclude`` like this:: and ``title`` fields, you would specify ``fields`` or ``exclude`` like this::
class AuthorAdmin(admin.ModelAdmin): class AuthorAdmin(admin.ModelAdmin):
fields = ('name', 'title') fields = ('name', 'title')
class AuthorAdmin(admin.ModelAdmin): class AuthorAdmin(admin.ModelAdmin):
exclude = ('birth_date',) exclude = ('birth_date',)
@ -254,30 +254,30 @@ that displays the ``__unicode__()`` representation of each object.
You have four possible values that can be used in ``list_display``: You have four possible values that can be used in ``list_display``:
* A field of the model. For example:: * A field of the model. 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 parameter for 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()
upper_case_name.short_description = 'Name' upper_case_name.short_description = 'Name'
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 behaves * A string representing an attribute on the ``ModelAdmin``. This behaves
same as the callable. For example:: same as the callable. For example::
class PersonAdmin(admin.ModelAdmin): class PersonAdmin(admin.ModelAdmin):
list_display = ('upper_case_name',) list_display = ('upper_case_name',)
def upper_case_name(self, obj): def upper_case_name(self, obj):
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 an attribute on the model. This behaves almost
the same as the callable, but ``self`` in this context is the model the same as the callable, but ``self`` in this context is the model
instance. Here's a full model example:: instance. Here's a full model example::
@ -311,7 +311,7 @@ A few special cases to note about ``list_display``:
callable, Django will HTML-escape the output by default. If you'd rather 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`` not escape the output of the method, give the method an ``allow_tags``
attribute whose value is ``True``. attribute whose value is ``True``.
Here's a full example model:: Here's a full example model::
class Person(models.Model): class Person(models.Model):
@ -400,9 +400,6 @@ the change list page::
list_display = ('first_name', 'last_name', 'birthday') list_display = ('first_name', 'last_name', 'birthday')
list_display_links = ('first_name', 'last_name') list_display_links = ('first_name', 'last_name')
Finally, note that in order to use ``list_display_links``, you must define
``list_display``, too.
``list_filter`` ``list_filter``
~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~
@ -662,11 +659,11 @@ the ability define your own form::
``MyArticleAdminForm`` can be defined anywhere as long as you import where ``MyArticleAdminForm`` can be defined anywhere as long as you import where
needed. Now within your form you can add your own custom validation for needed. Now within your form you can add your own custom validation for
any field:: any field::
class MyArticleAdminForm(forms.ModelForm): class MyArticleAdminForm(forms.ModelForm):
class Meta: class Meta:
model = Article model = Article
def clean_name(self): def clean_name(self):
# do something that validates your data # do something that validates your data
return self.cleaned_data["name"] return self.cleaned_data["name"]
@ -805,7 +802,7 @@ Working with Many-to-Many Intermediary Models
By default, admin widgets for many-to-many relations will be displayed inline By default, admin widgets for many-to-many relations will be displayed inline
on whichever model contains the actual reference to the ``ManyToManyField``. on whichever model contains the actual reference to the ``ManyToManyField``.
However, when you specify an intermediary model using the ``through`` However, when you specify an intermediary model using the ``through``
argument to a ``ManyToManyField``, the admin will not display a widget by argument to a ``ManyToManyField``, the admin will not display a widget by
default. This is because each instance of that intermediary model requires default. This is because each instance of that intermediary model requires
more information than could be displayed in a single widget, and the layout more information than could be displayed in a single widget, and the layout
required for multiple widgets will vary depending on the intermediate model. required for multiple widgets will vary depending on the intermediate model.
@ -816,7 +813,7 @@ models::
class Person(models.Model): class Person(models.Model):
name = models.CharField(max_length=128) name = models.CharField(max_length=128)
class Group(models.Model): class Group(models.Model):
name = models.CharField(max_length=128) name = models.CharField(max_length=128)
members = models.ManyToManyField(Person, through='Membership') members = models.ManyToManyField(Person, through='Membership')
@ -847,7 +844,7 @@ Now create admin views for the ``Person`` and ``Group`` models::
inlines = (MembershipInline,) inlines = (MembershipInline,)
Finally, register your ``Person`` and ``Group`` models with the admin site:: Finally, register your ``Person`` and ``Group`` models with the admin site::
admin.site.register(Person, PersonAdmin) admin.site.register(Person, PersonAdmin)
admin.site.register(Group, GroupAdmin) admin.site.register(Group, GroupAdmin)
@ -865,7 +862,7 @@ you have the following models::
content_type = models.ForeignKey(ContentType) content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField() object_id = models.PositiveIntegerField()
content_object = generic.GenericForeignKey("content_type", "object_id") content_object = generic.GenericForeignKey("content_type", "object_id")
class Product(models.Model): class Product(models.Model):
name = models.CharField(max_length=100) name = models.CharField(max_length=100)
@ -876,17 +873,17 @@ example app::
from django.contrib import admin from django.contrib import admin
from django.contrib.contenttypes import generic from django.contrib.contenttypes import generic
from myproject.myapp.models import Image, Product from myproject.myapp.models import Image, Product
class ImageInline(generic.GenericTabularInline): class ImageInline(generic.GenericTabularInline):
model = Image model = Image
class ProductAdmin(admin.ModelAdmin): class ProductAdmin(admin.ModelAdmin):
inlines = [ inlines = [
ImageInline, ImageInline,
] ]
admin.site.register(Product, ProductAdmin) admin.site.register(Product, ProductAdmin)
``django.contrib.contenttypes.generic`` provides both a ``GenericTabularInline`` ``django.contrib.contenttypes.generic`` provides both a ``GenericTabularInline``