Add missing imports and models to the examples in the admin documentation

This commit is contained in:
vkuzma 2013-05-19 12:42:35 +02:00 committed by Silvan Spross
parent 08b501e7d3
commit 1fe587d80b
2 changed files with 55 additions and 12 deletions

View File

@ -343,6 +343,7 @@ answer newbie questions, and generally made Django that much better:
David Krauth
Kevin Kubasik <kevin@kubasik.net>
kurtiss@meetro.com
Vladimir Kuzma <vladimirkuzma.ch@gmail.com>
Denis Kuzmichyov <kuzmichyov@gmail.com>
Panos Laganakos <panos.laganakos@gmail.com>
Nick Lane <nick.lane.au@gmail.com>

View File

@ -108,6 +108,8 @@ The ``ModelAdmin`` is very flexible. It has several options for dealing with
customizing the interface. All options are defined on the ``ModelAdmin``
subclass::
from django.contrib import admin
class AuthorAdmin(admin.ModelAdmin):
date_hierarchy = 'pub_date'
@ -157,6 +159,8 @@ subclass::
For example, let's consider the following model::
from django.db import models
class Author(models.Model):
name = models.CharField(max_length=100)
title = models.CharField(max_length=3)
@ -166,6 +170,8 @@ subclass::
and ``title`` fields, you would specify ``fields`` or ``exclude`` like
this::
from django.contrib import admin
class AuthorAdmin(admin.ModelAdmin):
fields = ('name', 'title')
@ -234,6 +240,8 @@ subclass::
A full example, taken from the
:class:`django.contrib.flatpages.models.FlatPage` model::
from django.contrib import admin
class FlatPageAdmin(admin.ModelAdmin):
fieldsets = (
(None, {
@ -356,6 +364,10 @@ subclass::
If your ``ModelForm`` and ``ModelAdmin`` both define an ``exclude``
option then ``ModelAdmin`` takes precedence::
from django import forms
from django.contrib import admin
from myapp.models import Person
class PersonForm(forms.ModelForm):
class Meta:
@ -459,6 +471,9 @@ subclass::
the same as the callable, but ``self`` in this context is the model
instance. Here's a full model example::
from django.db import models
from django.contrib import admin
class Person(models.Model):
name = models.CharField(max_length=50)
birthday = models.DateField()
@ -494,6 +509,8 @@ subclass::
Here's a full example model::
from django.db import models
from django.contrib import admin
from django.utils.html import format_html
class Person(models.Model):
@ -519,6 +536,9 @@ subclass::
Here's a full example model::
from django.db import models
from django.contrib import admin
class Person(models.Model):
first_name = models.CharField(max_length=50)
birthday = models.DateField()
@ -547,6 +567,8 @@ subclass::
For example::
from django.db import models
from django.contrib import admin
from django.utils.html import format_html
class Person(models.Model):
@ -634,13 +656,13 @@ subclass::
``BooleanField``, ``CharField``, ``DateField``, ``DateTimeField``,
``IntegerField``, ``ForeignKey`` or ``ManyToManyField``, for example::
class PersonAdmin(ModelAdmin):
class PersonAdmin(admin.ModelAdmin):
list_filter = ('is_staff', 'company')
Field names in ``list_filter`` can also span relations
using the ``__`` lookup, for example::
class PersonAdmin(UserAdmin):
class PersonAdmin(admin.UserAdmin):
list_filter = ('company__name',)
* a class inheriting from ``django.contrib.admin.SimpleListFilter``,
@ -650,10 +672,10 @@ subclass::
from datetime import date
from django.contrib import admin
from django.utils.translation import ugettext_lazy as _
from django.contrib.admin import SimpleListFilter
class DecadeBornListFilter(SimpleListFilter):
class DecadeBornListFilter(admin.SimpleListFilter):
# Human-readable title which will be displayed in the
# right admin sidebar just above the filter options.
title = _('decade born')
@ -689,7 +711,7 @@ subclass::
return queryset.filter(birthday__gte=date(1990, 1, 1),
birthday__lte=date(1999, 12, 31))
class PersonAdmin(ModelAdmin):
class PersonAdmin(admin.ModelAdmin):
list_filter = (DecadeBornListFilter,)
.. note::
@ -732,11 +754,9 @@ subclass::
element is a class inheriting from
``django.contrib.admin.FieldListFilter``, for example::
from django.contrib.admin import BooleanFieldListFilter
class PersonAdmin(ModelAdmin):
class PersonAdmin(admin.ModelAdmin):
list_filter = (
('is_staff', BooleanFieldListFilter),
('is_staff', admin.BooleanFieldListFilter),
)
.. note::
@ -746,7 +766,7 @@ subclass::
It is possible to specify a custom template for rendering a list filter::
class FilterWithCustomTemplate(SimpleListFilter):
class FilterWithCustomTemplate(admin.SimpleListFilter):
template = "custom_template.html"
See the default template provided by django (``admin/filter.html``) for
@ -876,10 +896,11 @@ subclass::
the admin interface to provide feedback on the status of the objects being
edited, for example::
from django.contrib import admin
from django.utils.html import format_html_join
from django.utils.safestring import mark_safe
class PersonAdmin(ModelAdmin):
class PersonAdmin(admin.ModelAdmin):
readonly_fields = ('address_report',)
def address_report(self, instance):
@ -1038,6 +1059,8 @@ templates used by the :class:`ModelAdmin` views:
For example to attach ``request.user`` to the object prior to saving::
from django.contrib import admin
class ArticleAdmin(admin.ModelAdmin):
def save_model(self, request, obj, form, change):
obj.user = request.user
@ -1071,7 +1094,7 @@ templates used by the :class:`ModelAdmin` views:
is expected to return a ``list`` or ``tuple`` for ordering similar
to the :attr:`ordering` attribute. For example::
class PersonAdmin(ModelAdmin):
class PersonAdmin(admin.ModelAdmin):
def get_ordering(self, request):
if request.user.is_superuser:
@ -1298,6 +1321,8 @@ templates used by the :class:`ModelAdmin` views:
Returns a :class:`~django.forms.ModelForm` class for use in the ``Formset``
on the changelist page. To use a custom form, for example::
from django import forms
class MyForm(forms.ModelForm):
pass
@ -1539,6 +1564,8 @@ information.
The admin interface has the ability to edit models on the same page as a
parent model. These are called inlines. Suppose you have these two models::
from django.db import models
class Author(models.Model):
name = models.CharField(max_length=100)
@ -1549,6 +1576,8 @@ information.
You can edit the books authored by an author on the author page. You add
inlines to a model by specifying them in a ``ModelAdmin.inlines``::
from django.contrib import admin
class BookInline(admin.TabularInline):
model = Book
@ -1682,6 +1711,8 @@ Working with a model with two or more foreign keys to the same parent model
It is sometimes possible to have more than one foreign key to the same model.
Take this model for instance::
from django.db import models
class Friendship(models.Model):
to_person = models.ForeignKey(Person, related_name="friends")
from_person = models.ForeignKey(Person, related_name="from_friends")
@ -1690,6 +1721,9 @@ If you wanted to display an inline on the ``Person`` admin add/change pages
you need to explicitly define the foreign key since it is unable to do so
automatically::
from django.contrib import admin
from myapp.models import Friendship
class FriendshipInline(admin.TabularInline):
model = Friendship
fk_name = "to_person"
@ -1712,6 +1746,8 @@ widgets with inlines.
Suppose we have the following models::
from django.db import models
class Person(models.Model):
name = models.CharField(max_length=128)
@ -1722,6 +1758,8 @@ Suppose we have the following models::
If you want to display many-to-many relations using an inline, you can do
so by defining an ``InlineModelAdmin`` object for the relationship::
from django.contrib import admin
class MembershipInline(admin.TabularInline):
model = Group.members.through
@ -1768,6 +1806,8 @@ However, we still want to be able to edit that information inline. Fortunately,
this is easy to do with inline admin models. Suppose we have the following
models::
from django.db import models
class Person(models.Model):
name = models.CharField(max_length=128)
@ -1816,6 +1856,8 @@ Using generic relations as an inline
It is possible to use an inline with generically related objects. Let's say
you have the following models::
from django.db import models
class Image(models.Model):
image = models.ImageField(upload_to="images")
content_type = models.ForeignKey(ContentType)