The modeladmin app is designed to offer you as much flexibility as possible in how your model and its objects are represented in Wagtail’s CMS. This page aims to provide you with some background information to help you gain a better understanding of what the app can do, and to point you in the right direction, depending on the kind of customisations you’re looking to make.
ModelAdmin class isn’t the same as Django’sModelAdmin class isn’t the same as Django’sWagtail’s ModelAdmin class is designed to be used in a similar way to Django’s class of the same name, and it often uses the same attribute and method names to achieve similar things. However, there are a few key differences:
panels and edit_handlers
In Wagtail, controlling which fields appear in add/edit forms for your Model, and defining how they are grouped and ordered, is achieved by adding a panels attribute, or edit_handler to your Model class. This remains the same whether your model is a Page type, a snippet, or just a standard Django Model. Because of this, Wagtail’s ModelAdmin class is mostly concerned with ‘listing’ configuration. For example, list_display, list_filter and search_fields attributes are present and support largely the same values as Django’s ModelAdmin class, while fields, fieldsets, exclude and other attributes you may be used to using to configure Django’s add/edit views, simply aren’t supported by Wagtail’s version.
While modeladmin’s listing view and it’s supported customisation options work in exactly the same way for all types of Model, when it comes to the other management views, the treatment differs depending on whether your ModelAdmin class is representing a page type model (that extends wagtailcore.models.Page) or not.
Pages in Wagtail have some unique properties, and require additional views, interface elements and general treatment in order to be managed effectively. For example, they have a tree structure that must be preserved properly as pages are added, deleted and moved around. They also have a revisions system, their own permission considerations, and the facility to preview changes before saving changes. Because of this added complexity, Wagtail provides its own specific views for managing any custom page types you might add to your project (whether you create a ModelAdmin class for them or not).
In order to deliver a consistent user experience, modeladmin simply redirects users to Wagtail’s existing page management views wherever possible. You should bear this in mind if you ever find yourself wanting to change what happens when pages of a certain type are added, deleted, published, or have some other action applied to them. Customising the CreateView or EditView for your page type Model (even if just to add an additional stylesheet or JavaScript), simply won’t have any effect, as those views are not used.
If you do find yourself needing to customise the add, edit or other behaviour for a page type model, you should take a look at the following part of the documentation: Hooks.
ModelAdmin class is ‘modular’Unlike Django’s class of the same name, wagtailadmin’s ModelAdmin acts primarily as a ‘controller’ class. While it does have a set of attributes and methods to enable you to configure how various components should treat your model, it has been deliberately designed to do as little work as possible by itself; it designates all of the real work to a set of separate, swappable components.
The theory is: If you want to do something differently, or add some functionality that modeladmin doesn’t already have, you can create new classes (or extend the ones provided by modeladmin) and easily configure your ModelAdmin class to use them instead of the defaults.
You should familiarise yourself with the attributes and methods supported by the ModelAdmin class, that allow you to change what is displayed in the IndexView. The following page should give you everything you need to get going: Customising IndexView - the listing view
The ModelAdmin class provides several attributes to enable you to easily add additional stylesheets and JavaScript to the admin interface for your model. Each attribute simply needs to be a list of paths to the files you want to include. If the path is for a file in your project’s static directory, then Wagtail will automatically prepend the path with STATIC_URL so that you don’t need to repeat it each time in your list of paths.
If you’d like to add styles or scripts to the IndexView, you should set the following attributes:
index_view_extra_css - Where each item is the path name of a pre-compiled stylesheet that you’d like to include.index_view_extra_js - Where each item is the path name of a JavaScript file that you’d like to include.If you’d like to do the same for CreateView and EditView, you should set the following attributes:
form_view_extra_css - Where each item is the path name of a pre-compiled stylesheet that you’d like to include.form_view_extra_js - Where each item is the path name of a JavaScript file that you’d like to include.And if you’re using the InspectView for your model, and want to do the same for that view, you should set the following attributes:
inspect_view_extra_css - Where each item is the path name of a pre-compiled stylesheet that you’d like to include.inspect_view_extra_js - Where each item is the path name of a JavaScript file that you’d like to include.For all modeladmin views, Wagtail looks for templates in the following folders within your project or app, before resorting to the defaults:
templates/modeladmin/app-name/model-name/templates/modeladmin/app-name/templates/modeladmin/So, to override the template used by IndexView for example, you’d create a new index.html template and put it in one of those locations. For example, if you wanted to do this for an ArticlePage model in a news app, you’d add your custom template as news/templates/modeladmin/news/articlepage/index.html.
For reference, modeladmin looks for templates with the following names for each view:
'index.html' for IndexView
'inspect.html' for InspectView
'create.html' for CreateView
'edit.html' for EditView
'delete.html' for DeleteView
'choose_parent.html' for ChooseParentView
To add extra information to a block within one of the above Wagtail templates, use Django’s {{ block.super }} within the {% block ... %} that you wish to extend. For example, if you wish to display an image in an edit form below the fields of the model that is being edited, you could do the following:
    {% extends "modeladmin/edit.html" %}
    {% load static %}
    {% block content %}
        {{ block.super }}
        <div>
            <img src="{% get_media_prefix %}{{ instance.image }}"/>
        </div>
    {% endblock %}
 If for any reason you’d rather bypass the above behaviour and explicitly specify a template for a specific view, you can set either of the following attributes on your ModelAdmin class:
index_template_name to specify a template for IndexView
inspect_template_name to specify a template for InspectView
create_template_name to specify a template for CreateView
edit_template_name to specify a template for EditView
delete_template_name to specify a template for DeleteView
choose_parent_template_name to specify a template for ChooseParentView
For all of the views offered by ModelAdmin, the class provides an attribute that you can override in order to tell it which class you’d like to use:
index_view_classinspect_view_classcreate_view_class (not used for ‘page type’ models)edit_view_class (not used for ‘page type’ models)delete_view_class (not used for ‘page type’ models)choose_parent_view_class (only used for ‘page type’ models)For example, if you’d like to create your own view class and use it for the IndexView, you would do the following:
    from wagtail.contrib.modeladmin.views import IndexView
    from wagtail.contrib.modeladmin.options import ModelAdmin
    from .models import MyModel
    class MyCustomIndexView(IndexView):
        # New functionality and existing method overrides added here
        ...
    class MyModelAdmin(ModelAdmin):
        model = MyModel
        index_view_class = MyCustomIndexView
 Or, if you have no need for any of IndexView’s existing functionality in your view and would rather create your own view from scratch, modeladmin will support that too. However, it’s highly recommended that you use modeladmin.views.WMABaseView as a base for your view. It’ll make integrating with your ModelAdmin class much easier and will provide a bunch of useful attributes and methods to get you started.
You can also use the url_helper to easily reverse URLs for any ModelAdmin see Reversing ModelAdmin URLs.
While ‘view classes’ are responsible for a lot of the work, there are also a number of other tasks that modeladmin must do regularly, that need to be handled in a consistent way, and in a number of different places. These tasks are designated to a set of simple classes (in modeladmin, these are termed ‘helper’ classes) and can be found in wagtail.contrib.modeladmin.helpers.
If you ever intend to write and use your own custom views with modeladmin, you should familiarise yourself with these helpers, as they are made available to views via the modeladmin.views.WMABaseView view.
There are three types of ‘helper class’:
The ModelAdmin class allows you to define and use your own helper classes by setting values on the following attributes:
ModelAdmin.url_helper_classBy default, the modeladmin.helpers.url.PageAdminURLHelper class is used when your model extends wagtailcore.models.Page, otherwise modeladmin.helpers.url.AdminURLHelper is used.
If you find that the above helper classes don’t work for your needs, you can easily create your own helper class by sub-classing AdminURLHelper or PageAdminURLHelper (if your model extends Wagtail’s Page model), and making any necessary additions/overrides.
Once your class is defined, set the url_helper_class attribute on your ModelAdmin class to use your custom URLHelper, like so:
    from wagtail.contrib.modeladmin.helpers import AdminURLHelper
    from wagtail.contrib.modeladmin.options import ModelAdmin, modeladmin_register
    from .models import MyModel
    class MyURLHelper(AdminURLHelper):
        ...
    class MyModelAdmin(ModelAdmin):
        model = MyModel
        url_helper_class = MyURLHelper
    modeladmin_register(MyModelAdmin)
 Or, if you have a more complicated use case, where simply setting that attribute isn’t possible (due to circular imports, for example) or doesn’t meet your needs, you can override the get_url_helper_class method, like so:
    class MyModelAdmin(ModelAdmin):
        model = MyModel
        def get_url_helper_class(self):
            if self.some_attribute is True:
                return MyURLHelper
            return AdminURLHelper
 ModelAdmin.permission_helper_classBy default, the modeladmin.helpers.permission.PagePermissionHelper class is used when your model extends wagtailcore.models.Page, otherwise modeladmin.helpers.permission.PermissionHelper is used.
If you find that the above helper classes don’t work for your needs, you can easily create your own helper class, by sub-classing PermissionHelper (or PagePermissionHelper if your model extends Wagtail’s Page model), and making any necessary additions/overrides. Once defined, you set the permission_helper_class attribute on your ModelAdmin class to use your custom class instead of the default, like so:
    from wagtail.contrib.modeladmin.helpers import PermissionHelper
    from wagtail.contrib.modeladmin.options import ModelAdmin, modeladmin_register
    from .models import MyModel
    class MyPermissionHelper(PermissionHelper):
        ...
    class MyModelAdmin(ModelAdmin):
        model = MyModel
        permission_helper_class = MyPermissionHelper
    modeladmin_register(MyModelAdmin)
 Or, if you have a more complicated use case, where simply setting an attribute isn’t possible or doesn’t meet your needs, you can override the get_permission_helper_class method, like so:
    class MyModelAdmin(ModelAdmin):
        model = MyModel
        def get_permission_helper_class(self):
            if self.some_attribute is True:
                return MyPermissionHelper
            return PermissionHelper
 As long as you sub-class modeladmin.views.WMABaseView (or one of the more ‘specific’ view classes) to create your custom view, instances of each helper should be available on instances of your class as:
self.url_helperself.permission_helperself.button_helperUnlike the other two, self.button_helper isn’t populated right away when the view is instantiated. In order to show the right buttons for the right users, ButtonHelper instances need to be ‘request aware’, so self.button_helper is only set once the view’s dispatch() method has run, which takes a HttpRequest object as an argument, from which the current user can be identified.
    © 2014-present Torchbox Ltd and individual contributors.
All rights are reserved.
Licensed under the BSD License.
    https://docs.wagtail.org/en/stable/reference/contrib/modeladmin/primer.html