2016-04-13 08:01:04 +02:00
=====================
`` ModelAdmin ``
=====================
2018-04-05 18:49:14 +02:00
The `` modeladmin `` module allows you to add any model in your project to the Wagtail admin. You can create customisable listing
2018-04-09 17:17:17 +02:00
pages for a model, including plain Django models, and add navigation elements so that a model can be accessed directly from the Wagtail admin. Simply extend the `` ModelAdmin `` class, override a few attributes to suit your needs, register it with Wagtail using an easy one-line `` modeladmin_register `` method (you can copy and paste from the examples below), and you're good to go. Your model doesn’ t need to extend `` Page `` or be registered as a `` Snippet `` , and it won’ t interfere with any of the existing admin functionality that Wagtail provides.
2016-04-13 08:01:04 +02:00
2016-08-08 22:19:33 +02:00
.. _modeladmin_feature_summary:
2016-04-13 08:01:04 +02:00
2016-08-08 22:19:33 +02:00
-------------------
Summary of features
-------------------
2016-04-13 08:01:04 +02:00
- A customisable list view, allowing you to control what values are displayed
2020-10-02 19:08:50 +02:00
for each row, available options for result filtering, default ordering,
2020-03-10 18:49:27 +01:00
spreadsheet downloads and more.
2016-04-13 08:01:04 +02:00
- Access your list views from the Wagtail admin menu easily with automatically
generated menu items, with automatic 'active item' highlighting. Control the
label text and icons used with easy-to-change attributes on your class.
- An additional `` ModelAdminGroup `` class, that allows you to group your
related models, and list them together in their own submenu, for a more
logical user experience.
- Simple, robust **add** and **edit** views for your non-Page models that use
the panel configurations defined on your model using Wagtail's edit panels.
- For Page models, the system directs to Wagtail's existing add and
edit views, and returns you back to the correct list page, for a seamless
experience.
- Full respect for permissions assigned to your Wagtail users and groups. Users
will only be able to do what you want them to!
- All you need to easily hook your `` ModelAdmin `` classes into Wagtail, taking
care of URL registration, menu changes, and registering any missing model
permissions, so that you can assign them to Groups.
- **Built to be customisable** - While `` modeladmin `` provides a solid
experience out of the box, you can easily use your own templates, and the
`` ModelAdmin `` class has a large number of methods that you can override or
extend, allowing you to customise the behaviour to a greater degree.
2016-08-08 22:19:33 +02:00
---------------------------------------------------
Want to know more about customising `` ModelAdmin `` ?
---------------------------------------------------
.. toctree ::
:maxdepth: 1
primer
menu_item
indexview
create_edit_delete_views
inspectview
chooseparentview
2019-03-13 23:42:24 +01:00
tips_and_tricks/index
2016-04-13 08:01:04 +02:00
.. _modeladmin_usage:
Installation
------------
Add `` wagtail.contrib.modeladmin `` to your `` INSTALLED_APPS `` :
.. code-block :: python
INSTALLED_APPS = [
...
'wagtail.contrib.modeladmin',
]
How to use
----------
.. _modeladmin_example_simple:
A simple example
^^^^^^^^^^^^^^^^
2018-09-13 20:56:02 +02:00
Let's say your website is for a local library. They have a model called
`` Book `` that appears across the site in many places. You can define a normal
Django model for it, then use ModelAdmin to create a menu in Wagtail's admin
to create, view, and edit `` Book `` entries.
`` models.py `` looks like this:
.. code-block :: python
2019-01-10 11:40:33 +01:00
from django.db import models
from wagtail.admin.edit_handlers import FieldPanel
2018-09-13 20:56:02 +02:00
class Book(models.Model):
title = models.CharField(max_length=255)
author = models.CharField(max_length=255)
cover_photo = models.ForeignKey(
'wagtailimages.Image',
2019-01-10 11:40:33 +01:00
null=True, blank=True,
2018-09-13 20:56:02 +02:00
on_delete=models.SET_NULL,
related_name='+'
)
panels = [
FieldPanel('title'),
FieldPanel('author'),
2021-11-05 12:31:37 +01:00
FieldPanel('cover_photo')
2018-09-13 20:56:02 +02:00
]
.. tip ::
2021-11-05 12:31:37 +01:00
You can specify panels like `` MultiFieldPanel `` within the `` panels `` attribute of the model.
This lets you use Wagtail-specific layouts in an otherwise traditional Django model.
2018-09-13 20:56:02 +02:00
2016-04-13 08:01:04 +02:00
`` wagtail_hooks.py `` in your app directory would look something like this:
.. code-block :: python
from wagtail.contrib.modeladmin.options import (
ModelAdmin, modeladmin_register)
2018-09-13 20:56:02 +02:00
from .models import Book
2016-04-13 08:01:04 +02:00
2018-09-13 20:56:02 +02:00
class BookAdmin(ModelAdmin):
model = Book
menu_label = 'Book' # ditch this to use verbose_name_plural from model
menu_icon = 'pilcrow' # change as required
2016-04-13 08:01:04 +02:00
menu_order = 200 # will put in 3rd place (000 being 1st, 100 2nd)
add_to_settings_menu = False # or True to add your model to the Settings sub-menu
2016-07-29 00:32:51 +02:00
exclude_from_explorer = False # or True to exclude pages of this type from Wagtail's explorer view
2018-09-13 20:56:02 +02:00
list_display = ('title', 'author')
list_filter = ('author',)
search_fields = ('title', 'author')
2016-04-13 08:01:04 +02:00
# Now you just need to register your customised ModelAdmin class with Wagtail
2018-09-13 20:56:02 +02:00
modeladmin_register(BookAdmin)
2016-04-13 08:01:04 +02:00
.. _modeladmin_example_complex:
A more complicated example
^^^^^^^^^^^^^^^^^^^^^^^^^^
2018-09-13 20:56:02 +02:00
In addition to `` Book `` , perhaps we also want to add `` Author `` and `` Genre ``
models to our app and display a menu item for each of them, too. Creating
lots of menus can add up quickly, so it might be a good idea to group related
menus together. This section show you how to create one menu called *Library*
which expands to show submenus for *Book* , *Author* , and *Genre* .
Assume we've defined `` Book `` , `` Author `` , and `` Genre `` models in
`` models.py `` .
2016-04-13 08:01:04 +02:00
`` wagtail_hooks.py `` in your app directory would look something like this:
.. code-block :: python
from wagtail.contrib.modeladmin.options import (
ModelAdmin, ModelAdminGroup, modeladmin_register)
from .models import (
2018-09-13 20:56:02 +02:00
Book, Author, Genre)
2016-04-13 08:01:04 +02:00
2018-09-13 20:56:02 +02:00
class BookAdmin(ModelAdmin):
model = Book
menu_label = 'Book' # ditch this to use verbose_name_plural from model
menu_icon = 'pilcrow' # change as required
list_display = ('title', 'author')
list_filter = ('genre', 'author')
search_fields = ('title', 'author')
2016-04-13 08:01:04 +02:00
2018-09-13 20:56:02 +02:00
class AuthorAdmin(ModelAdmin):
model = Author
menu_label = 'Author' # ditch this to use verbose_name_plural from model
menu_icon = 'user' # change as required
list_display = ('first_name', 'last_name')
list_filter = ('first_name', 'last_name')
search_fields = ('first_name', 'last_name')
2016-04-13 08:01:04 +02:00
2018-09-13 20:56:02 +02:00
class GenreAdmin(ModelAdmin):
model = Genre
menu_label = 'Genre' # ditch this to use verbose_name_plural from model
menu_icon = 'group' # change as required
list_display = ('name',)
list_filter = ('name',)
search_fields = ('name',)
2016-04-13 08:01:04 +02:00
2018-09-13 20:56:02 +02:00
class LibraryGroup(ModelAdminGroup):
menu_label = 'Library'
2016-04-13 08:01:04 +02:00
menu_icon = 'folder-open-inverse' # change as required
menu_order = 200 # will put in 3rd place (000 being 1st, 100 2nd)
2018-09-13 20:56:02 +02:00
items = (BookAdmin, AuthorAdmin, GenreAdmin)
2016-04-13 08:01:04 +02:00
# When using a ModelAdminGroup class to group several ModelAdmin classes together,
# you only need to register the ModelAdminGroup class with Wagtail:
2018-09-13 20:56:02 +02:00
modeladmin_register(LibraryGroup)
2016-04-13 08:01:04 +02:00
2019-01-30 10:30:15 +01:00
.. _modeladmin_multi_registration:
2016-04-13 08:01:04 +02:00
Registering multiple classes in one `` wagtail_hooks.py `` file
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2018-09-13 20:56:02 +02:00
Each time you call `` modeladmin_register(MyAdmin) `` it creates a new top-level
menu item in Wagtail's left sidebar. You can call this multiple times within
the same `` wagtail_hooks.py `` file if you want. The example below will create
3 top-level menus.
2016-04-13 08:01:04 +02:00
.. code-block :: python
2018-09-13 20:56:02 +02:00
class BookAdmin(ModelAdmin):
model = Book
2016-04-13 08:01:04 +02:00
...
2018-09-13 20:56:02 +02:00
class MovieAdmin(ModelAdmin):
model = MovieModel
2016-04-13 08:01:04 +02:00
...
2018-09-13 20:56:02 +02:00
class MusicAdminGroup(ModelAdminGroup):
2020-05-13 16:15:21 +02:00
menu_label = _("Music")
2018-09-13 20:56:02 +02:00
items = (AlbumAdmin, ArtistAdmin)
2016-04-13 08:01:04 +02:00
...
2018-09-13 20:56:02 +02:00
modeladmin_register(BookAdmin)
modeladmin_register(MovieAdmin)
modeladmin_register(MusicAdminGroup)