|
@@ -249,7 +249,7 @@ subclass::
|
|
|
from django.contrib import admin
|
|
|
|
|
|
class AuthorAdmin(admin.ModelAdmin):
|
|
|
- list_display = ('name', 'title', 'view_birth_date')
|
|
|
+ list_display = ['name', 'title', 'view_birth_date']
|
|
|
|
|
|
@admin.display(empty_value='???')
|
|
|
def view_birth_date(self, obj):
|
|
@@ -276,10 +276,10 @@ subclass::
|
|
|
from django.contrib import admin
|
|
|
|
|
|
class AuthorAdmin(admin.ModelAdmin):
|
|
|
- fields = ('name', 'title')
|
|
|
+ fields = ['name', 'title']
|
|
|
|
|
|
class AuthorAdmin(admin.ModelAdmin):
|
|
|
- exclude = ('birth_date',)
|
|
|
+ exclude = ['birth_date']
|
|
|
|
|
|
Since the Author model only has three fields, ``name``, ``title``, and
|
|
|
``birth_date``, the forms resulting from the above declarations will
|
|
@@ -294,7 +294,7 @@ subclass::
|
|
|
:class:`django.contrib.flatpages.models.FlatPage` model as follows::
|
|
|
|
|
|
class FlatPageAdmin(admin.ModelAdmin):
|
|
|
- fields = ('url', 'title', 'content')
|
|
|
+ fields = ['url', 'title', 'content']
|
|
|
|
|
|
In the above example, only the fields ``url``, ``title`` and ``content``
|
|
|
will be displayed, sequentially, in the form. ``fields`` can contain
|
|
@@ -314,7 +314,7 @@ subclass::
|
|
|
own line::
|
|
|
|
|
|
class FlatPageAdmin(admin.ModelAdmin):
|
|
|
- fields = (('url', 'title'), 'content')
|
|
|
+ fields = [('url', 'title'), 'content']
|
|
|
|
|
|
.. admonition:: Note
|
|
|
|
|
@@ -346,15 +346,15 @@ subclass::
|
|
|
from django.contrib import admin
|
|
|
|
|
|
class FlatPageAdmin(admin.ModelAdmin):
|
|
|
- fieldsets = (
|
|
|
+ fieldsets = [
|
|
|
(None, {
|
|
|
- 'fields': ('url', 'title', 'content', 'sites')
|
|
|
+ 'fields': ['url', 'title', 'content', 'sites'],
|
|
|
}),
|
|
|
('Advanced options', {
|
|
|
- 'classes': ('collapse',),
|
|
|
- 'fields': ('registration_required', 'template_name'),
|
|
|
+ 'classes': ['collapse'],
|
|
|
+ 'fields': ['registration_required', 'template_name'],
|
|
|
}),
|
|
|
- )
|
|
|
+ ]
|
|
|
|
|
|
This results in an admin page that looks like:
|
|
|
|
|
@@ -368,13 +368,13 @@ subclass::
|
|
|
The ``field_options`` dictionary can have the following keys:
|
|
|
|
|
|
* ``fields``
|
|
|
- A tuple of field names to display in this fieldset. This key is
|
|
|
+ A list or tuple of field names to display in this fieldset. This key is
|
|
|
required.
|
|
|
|
|
|
Example::
|
|
|
|
|
|
{
|
|
|
- 'fields': ('first_name', 'last_name', 'address', 'city', 'state'),
|
|
|
+ 'fields': ['first_name', 'last_name', 'address', 'city', 'state'],
|
|
|
}
|
|
|
|
|
|
As with the :attr:`~ModelAdmin.fields` option, to display multiple
|
|
@@ -383,7 +383,7 @@ subclass::
|
|
|
the same line::
|
|
|
|
|
|
{
|
|
|
- 'fields': (('first_name', 'last_name'), 'address', 'city', 'state'),
|
|
|
+ 'fields': [('first_name', 'last_name'), 'address', 'city', 'state'],
|
|
|
}
|
|
|
|
|
|
``fields`` can contain values defined in
|
|
@@ -399,7 +399,7 @@ subclass::
|
|
|
Example::
|
|
|
|
|
|
{
|
|
|
- 'classes': ('wide', 'extrapretty'),
|
|
|
+ 'classes': ['wide', 'extrapretty'],
|
|
|
}
|
|
|
|
|
|
Two useful classes defined by the default admin site stylesheet are
|
|
@@ -540,7 +540,7 @@ subclass::
|
|
|
|
|
|
Example::
|
|
|
|
|
|
- list_display = ('first_name', 'last_name')
|
|
|
+ list_display = ['first_name', 'last_name']
|
|
|
|
|
|
If you don't set ``list_display``, the admin site will display a single
|
|
|
column that displays the ``__str__()`` representation of each object.
|
|
@@ -552,7 +552,7 @@ subclass::
|
|
|
* The name of a model field. For example::
|
|
|
|
|
|
class PersonAdmin(admin.ModelAdmin):
|
|
|
- list_display = ('first_name', 'last_name')
|
|
|
+ list_display = ['first_name', 'last_name']
|
|
|
|
|
|
* A callable that accepts one argument, the model instance. For example::
|
|
|
|
|
@@ -561,13 +561,13 @@ subclass::
|
|
|
return ("%s %s" % (obj.first_name, obj.last_name)).upper()
|
|
|
|
|
|
class PersonAdmin(admin.ModelAdmin):
|
|
|
- list_display = (upper_case_name,)
|
|
|
+ list_display = [upper_case_name]
|
|
|
|
|
|
* A string representing a ``ModelAdmin`` method that accepts one argument,
|
|
|
the model instance. For example::
|
|
|
|
|
|
class PersonAdmin(admin.ModelAdmin):
|
|
|
- list_display = ('upper_case_name',)
|
|
|
+ list_display = ['upper_case_name']
|
|
|
|
|
|
@admin.display(description='Name')
|
|
|
def upper_case_name(self, obj):
|
|
@@ -588,7 +588,7 @@ subclass::
|
|
|
return '%d’s' % (self.birthday.year // 10 * 10)
|
|
|
|
|
|
class PersonAdmin(admin.ModelAdmin):
|
|
|
- list_display = ('name', 'decade_born_in')
|
|
|
+ list_display = ['name', 'decade_born_in']
|
|
|
|
|
|
A few special cases to note about ``list_display``:
|
|
|
|
|
@@ -630,7 +630,7 @@ subclass::
|
|
|
)
|
|
|
|
|
|
class PersonAdmin(admin.ModelAdmin):
|
|
|
- list_display = ('first_name', 'last_name', 'colored_name')
|
|
|
+ list_display = ['first_name', 'last_name', 'colored_name']
|
|
|
|
|
|
* As some examples have already demonstrated, when using a callable, a
|
|
|
model method, or a ``ModelAdmin`` method, you can customize the column's
|
|
@@ -654,7 +654,7 @@ subclass::
|
|
|
Or on a field level::
|
|
|
|
|
|
class PersonAdmin(admin.ModelAdmin):
|
|
|
- list_display = ('name', 'birth_date_view')
|
|
|
+ list_display = ['name', 'birth_date_view']
|
|
|
|
|
|
@admin.display(empty_value='unknown')
|
|
|
def birth_date_view(self, obj):
|
|
@@ -678,12 +678,12 @@ subclass::
|
|
|
return 1950 <= self.birthday.year < 1960
|
|
|
|
|
|
class PersonAdmin(admin.ModelAdmin):
|
|
|
- list_display = ('name', 'born_in_fifties')
|
|
|
+ list_display = ['name', 'born_in_fifties']
|
|
|
|
|
|
* The ``__str__()`` method is just as valid in ``list_display`` as any
|
|
|
other model method, so it's perfectly OK to do this::
|
|
|
|
|
|
- list_display = ('__str__', 'some_other_field')
|
|
|
+ list_display = ['__str__', 'some_other_field']
|
|
|
|
|
|
* Usually, elements of ``list_display`` that aren't actual database
|
|
|
fields can't be used in sorting (because Django does all the sorting
|
|
@@ -711,7 +711,7 @@ subclass::
|
|
|
)
|
|
|
|
|
|
class PersonAdmin(admin.ModelAdmin):
|
|
|
- list_display = ('first_name', 'colored_first_name')
|
|
|
+ list_display = ['first_name', 'colored_first_name']
|
|
|
|
|
|
The above will tell Django to order by the ``first_name`` field when
|
|
|
trying to sort by ``colored_first_name`` in the admin.
|
|
@@ -731,7 +731,7 @@ subclass::
|
|
|
author = models.ForeignKey(Person, on_delete=models.CASCADE)
|
|
|
|
|
|
class BlogAdmin(admin.ModelAdmin):
|
|
|
- list_display = ('title', 'author', 'author_first_name')
|
|
|
+ list_display = ['title', 'author', 'author_first_name']
|
|
|
|
|
|
@admin.display(ordering='author__first_name')
|
|
|
def author_first_name(self, obj):
|
|
@@ -766,7 +766,7 @@ subclass::
|
|
|
return self.first_name + ' ' + self.last_name
|
|
|
|
|
|
class PersonAdmin(admin.ModelAdmin):
|
|
|
- list_display = ('full_name',)
|
|
|
+ list_display = ['full_name']
|
|
|
|
|
|
Note that ``@property`` must be above ``@display``. If you're using the
|
|
|
old way -- setting the display-related attributes directly rather than
|
|
@@ -819,13 +819,13 @@ subclass::
|
|
|
linked on the change list page::
|
|
|
|
|
|
class PersonAdmin(admin.ModelAdmin):
|
|
|
- list_display = ('first_name', 'last_name', 'birthday')
|
|
|
- list_display_links = ('first_name', 'last_name')
|
|
|
+ list_display = ['first_name', 'last_name', 'birthday']
|
|
|
+ list_display_links = ['first_name', 'last_name']
|
|
|
|
|
|
In this example, the change list page grid will have no links::
|
|
|
|
|
|
class AuditEntryAdmin(admin.ModelAdmin):
|
|
|
- list_display = ('timestamp', 'message')
|
|
|
+ list_display = ['timestamp', 'message']
|
|
|
list_display_links = None
|
|
|
|
|
|
.. _admin-list-editable:
|
|
@@ -892,7 +892,7 @@ subclass::
|
|
|
``select_related`` as parameters. For example::
|
|
|
|
|
|
class ArticleAdmin(admin.ModelAdmin):
|
|
|
- list_select_related = ('author', 'category')
|
|
|
+ list_select_related = ['author', 'category']
|
|
|
|
|
|
will call ``select_related('author', 'category')``.
|
|
|
|
|
@@ -942,7 +942,7 @@ subclass::
|
|
|
fields it should prepopulate from::
|
|
|
|
|
|
class ArticleAdmin(admin.ModelAdmin):
|
|
|
- prepopulated_fields = {"slug": ("title",)}
|
|
|
+ prepopulated_fields = {"slug": ["title"]}
|
|
|
|
|
|
When set, the given fields will use a bit of JavaScript to populate from
|
|
|
the fields assigned. The main use for this functionality is to
|
|
@@ -1045,7 +1045,7 @@ subclass::
|
|
|
``ManyToManyField``::
|
|
|
|
|
|
class ArticleAdmin(admin.ModelAdmin):
|
|
|
- raw_id_fields = ("newspaper",)
|
|
|
+ raw_id_fields = ["newspaper"]
|
|
|
|
|
|
The ``raw_id_fields`` ``Input`` widget should contain a primary key if the
|
|
|
field is a ``ForeignKey`` or a comma separated list of values if the field
|
|
@@ -1081,7 +1081,7 @@ subclass::
|
|
|
from django.utils.safestring import mark_safe
|
|
|
|
|
|
class PersonAdmin(admin.ModelAdmin):
|
|
|
- readonly_fields = ('address_report',)
|
|
|
+ readonly_fields = ['address_report']
|
|
|
|
|
|
# description functions like a model field's verbose_name
|
|
|
@admin.display(description='Address')
|
|
@@ -1397,8 +1397,8 @@ templates used by the :class:`ModelAdmin` views:
|
|
|
For example, to search by ``name`` and ``age``, you could use::
|
|
|
|
|
|
class PersonAdmin(admin.ModelAdmin):
|
|
|
- list_display = ('name', 'age')
|
|
|
- search_fields = ('name',)
|
|
|
+ list_display = ['name', 'age']
|
|
|
+ search_fields = ['name']
|
|
|
|
|
|
def get_search_results(self, request, queryset, search_term):
|
|
|
queryset, may_have_duplicates = super().get_search_results(
|
|
@@ -1543,7 +1543,7 @@ templates used by the :class:`ModelAdmin` views:
|
|
|
filtering based on add, change, delete, and view permissions::
|
|
|
|
|
|
class MyModelAdmin(admin.ModelAdmin):
|
|
|
- inlines = (MyInline,)
|
|
|
+ inlines = [MyInline]
|
|
|
|
|
|
def get_inline_instances(self, request, obj=None):
|
|
|
return [inline(self.model, self.admin_site) for inline in self.inlines]
|
|
@@ -1736,12 +1736,12 @@ templates used by the :class:`ModelAdmin` views:
|
|
|
class MyModelAdmin(admin.ModelAdmin):
|
|
|
def formfield_for_choice_field(self, db_field, request, **kwargs):
|
|
|
if db_field.name == "status":
|
|
|
- kwargs['choices'] = (
|
|
|
+ kwargs['choices'] = [
|
|
|
('accepted', 'Accepted'),
|
|
|
('denied', 'Denied'),
|
|
|
- )
|
|
|
+ ]
|
|
|
if request.user.is_superuser:
|
|
|
- kwargs['choices'] += (('ready', 'Ready for deployment'),)
|
|
|
+ kwargs['choices'].append(('ready', 'Ready for deployment'))
|
|
|
return super().formfield_for_choice_field(db_field, request, **kwargs)
|
|
|
|
|
|
.. admonition:: Note
|
|
@@ -2063,9 +2063,9 @@ on your ``ModelAdmin``::
|
|
|
class ArticleAdmin(admin.ModelAdmin):
|
|
|
class Media:
|
|
|
css = {
|
|
|
- "all": ("my_styles.css",)
|
|
|
+ "all": ["my_styles.css"],
|
|
|
}
|
|
|
- js = ("my_code.js",)
|
|
|
+ js = ["my_code.js"]
|
|
|
|
|
|
The :doc:`staticfiles app </ref/contrib/staticfiles>` prepends
|
|
|
:setting:`STATIC_URL` (or :setting:`MEDIA_URL` if :setting:`STATIC_URL` is
|
|
@@ -2283,7 +2283,7 @@ The ``InlineModelAdmin`` class adds or customizes:
|
|
|
|
|
|
class BookInline(admin.TabularInline):
|
|
|
model = Book
|
|
|
- raw_id_fields = ("pages",)
|
|
|
+ raw_id_fields = ["pages"]
|
|
|
|
|
|
|
|
|
.. attribute:: InlineModelAdmin.template
|
|
@@ -2451,7 +2451,7 @@ so by defining an ``InlineModelAdmin`` object for the relationship::
|
|
|
inlines = [
|
|
|
MembershipInline,
|
|
|
]
|
|
|
- exclude = ('members',)
|
|
|
+ exclude = ['members']
|
|
|
|
|
|
There are two features worth noting in this example.
|
|
|
|
|
@@ -2518,10 +2518,10 @@ customized using any of the options available to ``InlineModelAdmin`` classes.
|
|
|
Now create admin views for the ``Person`` and ``Group`` models::
|
|
|
|
|
|
class PersonAdmin(admin.ModelAdmin):
|
|
|
- inlines = (MembershipInline,)
|
|
|
+ inlines = [MembershipInline]
|
|
|
|
|
|
class GroupAdmin(admin.ModelAdmin):
|
|
|
- inlines = (MembershipInline,)
|
|
|
+ inlines = [MembershipInline]
|
|
|
|
|
|
Finally, register your ``Person`` and ``Group`` models with the admin site::
|
|
|
|