Add missing imports and models to the examples in the admin documentation
This commit is contained in:
parent
08b501e7d3
commit
1fe587d80b
1
AUTHORS
1
AUTHORS
|
@ -343,6 +343,7 @@ answer newbie questions, and generally made Django that much better:
|
||||||
David Krauth
|
David Krauth
|
||||||
Kevin Kubasik <kevin@kubasik.net>
|
Kevin Kubasik <kevin@kubasik.net>
|
||||||
kurtiss@meetro.com
|
kurtiss@meetro.com
|
||||||
|
Vladimir Kuzma <vladimirkuzma.ch@gmail.com>
|
||||||
Denis Kuzmichyov <kuzmichyov@gmail.com>
|
Denis Kuzmichyov <kuzmichyov@gmail.com>
|
||||||
Panos Laganakos <panos.laganakos@gmail.com>
|
Panos Laganakos <panos.laganakos@gmail.com>
|
||||||
Nick Lane <nick.lane.au@gmail.com>
|
Nick Lane <nick.lane.au@gmail.com>
|
||||||
|
|
|
@ -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``
|
customizing the interface. All options are defined on the ``ModelAdmin``
|
||||||
subclass::
|
subclass::
|
||||||
|
|
||||||
|
from django.contrib import admin
|
||||||
|
|
||||||
class AuthorAdmin(admin.ModelAdmin):
|
class AuthorAdmin(admin.ModelAdmin):
|
||||||
date_hierarchy = 'pub_date'
|
date_hierarchy = 'pub_date'
|
||||||
|
|
||||||
|
@ -157,6 +159,8 @@ subclass::
|
||||||
|
|
||||||
For example, let's consider the following model::
|
For example, let's consider the following model::
|
||||||
|
|
||||||
|
from django.db import models
|
||||||
|
|
||||||
class Author(models.Model):
|
class Author(models.Model):
|
||||||
name = models.CharField(max_length=100)
|
name = models.CharField(max_length=100)
|
||||||
title = models.CharField(max_length=3)
|
title = models.CharField(max_length=3)
|
||||||
|
@ -166,6 +170,8 @@ subclass::
|
||||||
and ``title`` fields, you would specify ``fields`` or ``exclude`` like
|
and ``title`` fields, you would specify ``fields`` or ``exclude`` like
|
||||||
this::
|
this::
|
||||||
|
|
||||||
|
from django.contrib import admin
|
||||||
|
|
||||||
class AuthorAdmin(admin.ModelAdmin):
|
class AuthorAdmin(admin.ModelAdmin):
|
||||||
fields = ('name', 'title')
|
fields = ('name', 'title')
|
||||||
|
|
||||||
|
@ -234,6 +240,8 @@ subclass::
|
||||||
A full example, taken from the
|
A full example, taken from the
|
||||||
:class:`django.contrib.flatpages.models.FlatPage` model::
|
:class:`django.contrib.flatpages.models.FlatPage` model::
|
||||||
|
|
||||||
|
from django.contrib import admin
|
||||||
|
|
||||||
class FlatPageAdmin(admin.ModelAdmin):
|
class FlatPageAdmin(admin.ModelAdmin):
|
||||||
fieldsets = (
|
fieldsets = (
|
||||||
(None, {
|
(None, {
|
||||||
|
@ -356,6 +364,10 @@ subclass::
|
||||||
If your ``ModelForm`` and ``ModelAdmin`` both define an ``exclude``
|
If your ``ModelForm`` and ``ModelAdmin`` both define an ``exclude``
|
||||||
option then ``ModelAdmin`` takes precedence::
|
option then ``ModelAdmin`` takes precedence::
|
||||||
|
|
||||||
|
from django import forms
|
||||||
|
from django.contrib import admin
|
||||||
|
from myapp.models import Person
|
||||||
|
|
||||||
class PersonForm(forms.ModelForm):
|
class PersonForm(forms.ModelForm):
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
|
@ -459,6 +471,9 @@ subclass::
|
||||||
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::
|
||||||
|
|
||||||
|
from django.db import models
|
||||||
|
from django.contrib import admin
|
||||||
|
|
||||||
class Person(models.Model):
|
class Person(models.Model):
|
||||||
name = models.CharField(max_length=50)
|
name = models.CharField(max_length=50)
|
||||||
birthday = models.DateField()
|
birthday = models.DateField()
|
||||||
|
@ -494,6 +509,8 @@ subclass::
|
||||||
|
|
||||||
Here's a full example model::
|
Here's a full example model::
|
||||||
|
|
||||||
|
from django.db import models
|
||||||
|
from django.contrib import admin
|
||||||
from django.utils.html import format_html
|
from django.utils.html import format_html
|
||||||
|
|
||||||
class Person(models.Model):
|
class Person(models.Model):
|
||||||
|
@ -519,6 +536,9 @@ subclass::
|
||||||
|
|
||||||
Here's a full example model::
|
Here's a full example model::
|
||||||
|
|
||||||
|
from django.db import models
|
||||||
|
from django.contrib import admin
|
||||||
|
|
||||||
class Person(models.Model):
|
class Person(models.Model):
|
||||||
first_name = models.CharField(max_length=50)
|
first_name = models.CharField(max_length=50)
|
||||||
birthday = models.DateField()
|
birthday = models.DateField()
|
||||||
|
@ -547,6 +567,8 @@ subclass::
|
||||||
|
|
||||||
For example::
|
For example::
|
||||||
|
|
||||||
|
from django.db import models
|
||||||
|
from django.contrib import admin
|
||||||
from django.utils.html import format_html
|
from django.utils.html import format_html
|
||||||
|
|
||||||
class Person(models.Model):
|
class Person(models.Model):
|
||||||
|
@ -634,13 +656,13 @@ subclass::
|
||||||
``BooleanField``, ``CharField``, ``DateField``, ``DateTimeField``,
|
``BooleanField``, ``CharField``, ``DateField``, ``DateTimeField``,
|
||||||
``IntegerField``, ``ForeignKey`` or ``ManyToManyField``, for example::
|
``IntegerField``, ``ForeignKey`` or ``ManyToManyField``, for example::
|
||||||
|
|
||||||
class PersonAdmin(ModelAdmin):
|
class PersonAdmin(admin.ModelAdmin):
|
||||||
list_filter = ('is_staff', 'company')
|
list_filter = ('is_staff', 'company')
|
||||||
|
|
||||||
Field names in ``list_filter`` can also span relations
|
Field names in ``list_filter`` can also span relations
|
||||||
using the ``__`` lookup, for example::
|
using the ``__`` lookup, for example::
|
||||||
|
|
||||||
class PersonAdmin(UserAdmin):
|
class PersonAdmin(admin.UserAdmin):
|
||||||
list_filter = ('company__name',)
|
list_filter = ('company__name',)
|
||||||
|
|
||||||
* a class inheriting from ``django.contrib.admin.SimpleListFilter``,
|
* a class inheriting from ``django.contrib.admin.SimpleListFilter``,
|
||||||
|
@ -650,10 +672,10 @@ subclass::
|
||||||
|
|
||||||
from datetime import date
|
from datetime import date
|
||||||
|
|
||||||
|
from django.contrib import admin
|
||||||
from django.utils.translation import ugettext_lazy as _
|
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
|
# Human-readable title which will be displayed in the
|
||||||
# right admin sidebar just above the filter options.
|
# right admin sidebar just above the filter options.
|
||||||
title = _('decade born')
|
title = _('decade born')
|
||||||
|
@ -689,7 +711,7 @@ subclass::
|
||||||
return queryset.filter(birthday__gte=date(1990, 1, 1),
|
return queryset.filter(birthday__gte=date(1990, 1, 1),
|
||||||
birthday__lte=date(1999, 12, 31))
|
birthday__lte=date(1999, 12, 31))
|
||||||
|
|
||||||
class PersonAdmin(ModelAdmin):
|
class PersonAdmin(admin.ModelAdmin):
|
||||||
list_filter = (DecadeBornListFilter,)
|
list_filter = (DecadeBornListFilter,)
|
||||||
|
|
||||||
.. note::
|
.. note::
|
||||||
|
@ -732,11 +754,9 @@ subclass::
|
||||||
element is a class inheriting from
|
element is a class inheriting from
|
||||||
``django.contrib.admin.FieldListFilter``, for example::
|
``django.contrib.admin.FieldListFilter``, for example::
|
||||||
|
|
||||||
from django.contrib.admin import BooleanFieldListFilter
|
class PersonAdmin(admin.ModelAdmin):
|
||||||
|
|
||||||
class PersonAdmin(ModelAdmin):
|
|
||||||
list_filter = (
|
list_filter = (
|
||||||
('is_staff', BooleanFieldListFilter),
|
('is_staff', admin.BooleanFieldListFilter),
|
||||||
)
|
)
|
||||||
|
|
||||||
.. note::
|
.. note::
|
||||||
|
@ -746,7 +766,7 @@ subclass::
|
||||||
|
|
||||||
It is possible to specify a custom template for rendering a list filter::
|
It is possible to specify a custom template for rendering a list filter::
|
||||||
|
|
||||||
class FilterWithCustomTemplate(SimpleListFilter):
|
class FilterWithCustomTemplate(admin.SimpleListFilter):
|
||||||
template = "custom_template.html"
|
template = "custom_template.html"
|
||||||
|
|
||||||
See the default template provided by django (``admin/filter.html``) for
|
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
|
the admin interface to provide feedback on the status of the objects being
|
||||||
edited, for example::
|
edited, for example::
|
||||||
|
|
||||||
|
from django.contrib import admin
|
||||||
from django.utils.html import format_html_join
|
from django.utils.html import format_html_join
|
||||||
from django.utils.safestring import mark_safe
|
from django.utils.safestring import mark_safe
|
||||||
|
|
||||||
class PersonAdmin(ModelAdmin):
|
class PersonAdmin(admin.ModelAdmin):
|
||||||
readonly_fields = ('address_report',)
|
readonly_fields = ('address_report',)
|
||||||
|
|
||||||
def address_report(self, instance):
|
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::
|
For example to attach ``request.user`` to the object prior to saving::
|
||||||
|
|
||||||
|
from django.contrib import admin
|
||||||
|
|
||||||
class ArticleAdmin(admin.ModelAdmin):
|
class ArticleAdmin(admin.ModelAdmin):
|
||||||
def save_model(self, request, obj, form, change):
|
def save_model(self, request, obj, form, change):
|
||||||
obj.user = request.user
|
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
|
is expected to return a ``list`` or ``tuple`` for ordering similar
|
||||||
to the :attr:`ordering` attribute. For example::
|
to the :attr:`ordering` attribute. For example::
|
||||||
|
|
||||||
class PersonAdmin(ModelAdmin):
|
class PersonAdmin(admin.ModelAdmin):
|
||||||
|
|
||||||
def get_ordering(self, request):
|
def get_ordering(self, request):
|
||||||
if request.user.is_superuser:
|
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``
|
Returns a :class:`~django.forms.ModelForm` class for use in the ``Formset``
|
||||||
on the changelist page. To use a custom form, for example::
|
on the changelist page. To use a custom form, for example::
|
||||||
|
|
||||||
|
from django import forms
|
||||||
|
|
||||||
class MyForm(forms.ModelForm):
|
class MyForm(forms.ModelForm):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
@ -1539,6 +1564,8 @@ information.
|
||||||
The admin interface has the ability to edit models on the same page as a
|
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::
|
parent model. These are called inlines. Suppose you have these two models::
|
||||||
|
|
||||||
|
from django.db import models
|
||||||
|
|
||||||
class Author(models.Model):
|
class Author(models.Model):
|
||||||
name = models.CharField(max_length=100)
|
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
|
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``::
|
inlines to a model by specifying them in a ``ModelAdmin.inlines``::
|
||||||
|
|
||||||
|
from django.contrib import admin
|
||||||
|
|
||||||
class BookInline(admin.TabularInline):
|
class BookInline(admin.TabularInline):
|
||||||
model = Book
|
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.
|
It is sometimes possible to have more than one foreign key to the same model.
|
||||||
Take this model for instance::
|
Take this model for instance::
|
||||||
|
|
||||||
|
from django.db import models
|
||||||
|
|
||||||
class Friendship(models.Model):
|
class Friendship(models.Model):
|
||||||
to_person = models.ForeignKey(Person, related_name="friends")
|
to_person = models.ForeignKey(Person, related_name="friends")
|
||||||
from_person = models.ForeignKey(Person, related_name="from_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
|
you need to explicitly define the foreign key since it is unable to do so
|
||||||
automatically::
|
automatically::
|
||||||
|
|
||||||
|
from django.contrib import admin
|
||||||
|
from myapp.models import Friendship
|
||||||
|
|
||||||
class FriendshipInline(admin.TabularInline):
|
class FriendshipInline(admin.TabularInline):
|
||||||
model = Friendship
|
model = Friendship
|
||||||
fk_name = "to_person"
|
fk_name = "to_person"
|
||||||
|
@ -1712,6 +1746,8 @@ widgets with inlines.
|
||||||
|
|
||||||
Suppose we have the following models::
|
Suppose we have the following models::
|
||||||
|
|
||||||
|
from django.db import models
|
||||||
|
|
||||||
class Person(models.Model):
|
class Person(models.Model):
|
||||||
name = models.CharField(max_length=128)
|
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
|
If you want to display many-to-many relations using an inline, you can do
|
||||||
so by defining an ``InlineModelAdmin`` object for the relationship::
|
so by defining an ``InlineModelAdmin`` object for the relationship::
|
||||||
|
|
||||||
|
from django.contrib import admin
|
||||||
|
|
||||||
class MembershipInline(admin.TabularInline):
|
class MembershipInline(admin.TabularInline):
|
||||||
model = Group.members.through
|
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
|
this is easy to do with inline admin models. Suppose we have the following
|
||||||
models::
|
models::
|
||||||
|
|
||||||
|
from django.db import models
|
||||||
|
|
||||||
class Person(models.Model):
|
class Person(models.Model):
|
||||||
name = models.CharField(max_length=128)
|
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
|
It is possible to use an inline with generically related objects. Let's say
|
||||||
you have the following models::
|
you have the following models::
|
||||||
|
|
||||||
|
from django.db import models
|
||||||
|
|
||||||
class Image(models.Model):
|
class Image(models.Model):
|
||||||
image = models.ImageField(upload_to="images")
|
image = models.ImageField(upload_to="images")
|
||||||
content_type = models.ForeignKey(ContentType)
|
content_type = models.ForeignKey(ContentType)
|
||||||
|
|
Loading…
Reference in New Issue