primer.rst 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. ===================================
  2. ``modeladmin`` customisation primer
  3. ===================================
  4. The ``modeladmin`` app is designed to offer you as much flexibility as possible
  5. in how your model and its objects are represented in Wagtail's CMS. This page
  6. aims to provide you with some background information to help you gain a better
  7. understanding of what the app can do, and to point you in the right direction,
  8. depending on the kind of customisations you're looking to make.
  9. .. contents::
  10. :local:
  11. :depth: 1
  12. ---------------------------------------------------------
  13. Wagtail's ``ModelAdmin`` class isn't the same as Django's
  14. ---------------------------------------------------------
  15. Wagtail's ``ModelAdmin`` class is designed to be used in a similar way to
  16. Django's class of the same name, and it often uses the same attribute and method
  17. names to achieve similar things. However, there are a few key differences:
  18. Add & edit forms are still defined by ``panels`` and ``edit_handlers``
  19. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  20. In Wagtail, controlling which fields appear in add/edit forms for your
  21. ``Model``, and defining how they are grouped and ordered, is achieved by
  22. adding a ``panels`` attribute, or `edit_handler` to your ``Model`` class.
  23. This remains the same whether your model is a ``Page`` type, a snippet, or
  24. just a standard Django ``Model``. Because of this, Wagtail's ``ModelAdmin``
  25. class is mostly concerned with 'listing' configuration. For example,
  26. ``list_display``, ``list_filter`` and ``search_fields`` attributes are
  27. present and support largely the same values as Django's ModelAdmin class,
  28. while `fields`, `fieldsets`, `exclude` and other attributes you may be used
  29. to using to configure Django's add/edit views, simply aren't supported by
  30. Wagtail's version.
  31. 'Page type' models need to be treated differently from other models
  32. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  33. While ``modeladmin``'s listing view and it's supported customisation
  34. options work in exactly the same way for all types of ``Model``, when it
  35. comes to the other management views, the treatment differs depending on
  36. whether your ModelAdmin class is representing a page type model (that
  37. extends ``wagtailcore.models.Page``) or not.
  38. Pages in Wagtail have some unique properties, and require additional views,
  39. interface elements and general treatment in order to be managed
  40. effectively. For example, they have a tree structure that must be preserved
  41. properly as pages are added, deleted and moved around. They also have a
  42. revisions system, their own permission considerations, and the facility to
  43. preview changes before saving changes. Because of this added complexity,
  44. Wagtail provides its own specific views for managing any custom page types
  45. you might add to your project (whether you create a ``ModelAdmin`` class
  46. for them or not).
  47. In order to deliver a consistent user experience, ``modeladmin``
  48. simply redirects users to Wagtail's existing page management views wherever
  49. possible. You should bear this in mind if you ever find yourself wanting to
  50. change what happens when pages of a certain type are added, deleted,
  51. published, or have some other action applied to them. Customising the
  52. ``CreateView`` or ``EditView`` for your page type ``Model`` (even if just
  53. to add an additional stylesheet or JavaScript), simply won't have any
  54. effect, as those views are not used.
  55. If you do find yourself needing to customise the add, edit or other
  56. behaviour for a page type model, you should take a look at the following
  57. part of the documentation: :ref:`admin_hooks`.
  58. Wagtail's ``ModelAdmin`` class is 'modular'
  59. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  60. Unlike Django's class of the same name, wagtailadmin's ``ModelAmin`` acts
  61. primarily as a 'controller' class. While it does have a set of attributes
  62. and methods to enable you to configure how various components should treat
  63. your model, it has been deliberately designed to do as little work as
  64. possible by itself; it designates all of the real work to a set of
  65. separate, swappable components.
  66. The theory is: If you want to do something differently, or add some
  67. functionality that ``modeladmin`` doesn't already have, you can create new
  68. classes (or extend the ones provided by ``modeladmin``) and easily
  69. configure your ``ModelAdmin`` class to use them instead of the defaults.
  70. - Learn more about :ref:`modeladmin_overriding_views`
  71. - Learn more about :ref:`modeladmin_overriding_helper_classes`
  72. ------------------------------------
  73. Changing what appears in the listing
  74. ------------------------------------
  75. You should familiarise yourself with the attributes and methods supported by
  76. the ``ModelAdmin`` class, that allow you to change what is displayed in the
  77. ``IndexView``. The following page should give you everything you need to get
  78. going: :doc:`indexview`
  79. .. _modeladmin_adding_css_and_js:
  80. -----------------------------------------------
  81. Adding additional stylesheets and/or JavaScript
  82. -----------------------------------------------
  83. The ``ModelAdmin`` class provides several attributes to enable you to easily
  84. add additional stylesheets and JavaScript to the admin interface for your
  85. model. Each attribute simply needs to be a list of paths to the files you
  86. want to include. If the path is for a file in your project's static directory,
  87. 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.
  88. If you'd like to add styles or scripts to the ``IndexView``, you should set the
  89. following attributes:
  90. - ``index_view_extra_css`` - Where each item is the path name of a
  91. pre-compiled stylesheet that you'd like to include.
  92. - ``index_view_extra_js`` - Where each item is the path name of a JavaScript
  93. file that you'd like to include.
  94. If you'd like to do the same for ``CreateView`` and ``EditView``, you should
  95. set the following attributes:
  96. - ``form_view_extra_css`` - Where each item is the path name of a
  97. pre-compiled stylesheet that you'd like to include.
  98. - ``form_view_extra_js`` - Where each item is the path name of a JavaScript
  99. file that you'd like to include.
  100. And if you're using the ``InspectView`` for your model, and want to do the same
  101. for that view, you should set the following attributes:
  102. - ``inspect_view_extra_css`` - Where each item is the path name of a
  103. pre-compiled stylesheet that you'd like to include.
  104. - ``inspect_view_extra_js`` - Where each item is the path name of a JavaScript
  105. file that you'd like to include.
  106. .. _modeladmin_overriding_templates:
  107. --------------------
  108. Overriding templates
  109. --------------------
  110. For all modeladmin views, Wagtail looks for templates in the following folders
  111. within your project or app, before resorting to the defaults:
  112. 1. ``templates/modeladmin/app-name/model-name/``
  113. 2. ``templates/modeladmin/app-name/``
  114. 3. ``templates/modeladmin/``
  115. So, to override the template used by ``IndexView`` for example, you'd create a
  116. new ``index.html`` template and put it in one of those locations. For example,
  117. if you wanted to do this for an ``ArticlePage`` model in a ``news`` app, you'd
  118. add your custom template as ``news/templates/modeladmin/news/articlepage/index.html``.
  119. For reference, ``modeladmin`` looks for templates with the following names for
  120. each view:
  121. - ``'index.html'`` for ``IndexView``
  122. - ``'inspect.html'`` for ``InspectView``
  123. - ``'create.html'`` for ``CreateView``
  124. - ``'edit.html'`` for ``EditView``
  125. - ``'delete.html'`` for ``DeleteView``
  126. - ``'choose_parent.html'`` for ``ChooseParentView``
  127. 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:
  128. .. code-block:: html+django
  129. {% extends "modeladmin/edit.html" %}
  130. {% load static %}
  131. {% block content %}
  132. {{ block.super }}
  133. <div class="object">
  134. <img src="{% get_media_prefix %}{{ instance.image }}"/>
  135. </div>
  136. {% endblock %}
  137. If for any reason you'd rather bypass the above behaviour and explicitly specify a
  138. template for a specific view, you can set either of the following attributes
  139. on your ``ModelAdmin`` class:
  140. - ``index_template_name`` to specify a template for ``IndexView``
  141. - ``inspect_template_name`` to specify a template for ``InspectView``
  142. - ``create_template_name`` to specify a template for ``CreateView``
  143. - ``edit_template_name`` to specify a template for ``EditView``
  144. - ``delete_template_name`` to specify a template for ``DeleteView``
  145. - ``choose_parent_template_name`` to specify a template for ``ChooseParentView``
  146. .. _modeladmin_overriding_views:
  147. ----------------
  148. Overriding views
  149. ----------------
  150. For all of the views offered by ``ModelAdmin``, the class provides an attribute
  151. that you can override in order to tell it which class you'd like to use:
  152. - ``index_view_class``
  153. - ``inspect_view_class``
  154. - ``create_view_class`` (not used for 'page type' models)
  155. - ``edit_view_class`` (not used for 'page type' models)
  156. - ``delete_view_class`` (not used for 'page type' models)
  157. - ``choose_parent_view_class`` (only used for 'page type' models)
  158. For example, if you'd like to create your own view class and use it for the
  159. ``IndexView``, you would do the following:
  160. .. code-block:: python
  161. from wagtail.contrib.modeladmin.views import IndexView
  162. from wagtail.contrib.modeladmin.options import ModelAdmin
  163. from .models import MyModel
  164. class MyCustomIndexView(IndexView):
  165. # New functionality and existing method overrides added here
  166. ...
  167. class MyModelAdmin(ModelAdmin):
  168. model = MyModel
  169. index_view_class = MyCustomIndexView
  170. Or, if you have no need for any of ``IndexView``'s existing functionality in
  171. your view and would rather create your own view from scratch, ``modeladmin``
  172. will support that too. However, it's highly recommended that you use
  173. ``modeladmin.views.WMABaseView`` as a base for your view. It'll make
  174. integrating with your ``ModelAdmin`` class much easier and will provide a bunch of
  175. useful attributes and methods to get you started.
  176. You can also use the url_helper to easily reverse URLs for any ModelAdmin see :ref:`modeladmin_reversing_urls`.
  177. .. _modeladmin_overriding_helper_classes:
  178. -------------------------
  179. Overriding helper classes
  180. -------------------------
  181. While 'view classes' are responsible for a lot of the work, there are also
  182. a number of other tasks that ``modeladmin`` must do regularly, that need to be
  183. handled in a consistent way, and in a number of different places. These tasks
  184. are designated to a set of simple classes (in ``modeladmin``, these are termed
  185. 'helper' classes) and can be found in ``wagtail.contrib.modeladmin.helpers``.
  186. If you ever intend to write and use your own custom views with ``modeladmin``,
  187. you should familiarise yourself with these helpers, as they are made available
  188. to views via the ``modeladmin.views.WMABaseView`` view.
  189. There are three types of 'helper class':
  190. - **URL helpers** - That help with the consistent generation, naming and
  191. referencing of urls.
  192. - **Permission helpers** - That help with ensuring only users with sufficient
  193. permissions can perform certain actions, or see options to perform those
  194. actions.
  195. - **Button helpers** - That, with the help of the other two, helps with the
  196. generation of buttons for use in a number of places.
  197. The ``ModelAdmin`` class allows you to define and use your own helper classes
  198. by setting values on the following attributes:
  199. .. _modeladmin_url_helper_class:
  200. ``ModelAdmin.url_helper_class``
  201. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  202. By default, the ``modeladmin.helpers.url.PageAdminURLHelper`` class is used
  203. when your model extends ``wagtailcore.models.Page``, otherwise
  204. ``modeladmin.helpers.url.AdminURLHelper`` is used.
  205. If you find that the above helper classes don't work for your needs, you can
  206. easily create your own helper class by sub-classing ``AdminURLHelper`` or
  207. ``PageAdminURLHelper`` (if your model extends Wagtail's ``Page`` model), and
  208. making any necessary additions/overrides.
  209. Once your class is defined, set the ``url_helper_class`` attribute on
  210. your ``ModelAdmin`` class to use your custom URLHelper, like so:
  211. .. code-block:: python
  212. from wagtail.contrib.modeladmin.helpers import AdminURLHelper
  213. from wagtail.contrib.modeladmin.options import ModelAdmin, modeladmin_register
  214. from .models import MyModel
  215. class MyURLHelper(AdminURLHelper):
  216. ...
  217. class MyModelAdmin(ModelAdmin):
  218. model = MyModel
  219. url_helper_class = MyURLHelper
  220. modeladmin_register(MyModelAdmin)
  221. Or, if you have a more complicated use case, where simply setting that
  222. attribute isn't possible (due to circular imports, for example) or doesn't
  223. meet your needs, you can override the ``get_url_helper_class`` method, like
  224. so:
  225. .. code-block:: python
  226. class MyModelAdmin(ModelAdmin):
  227. model = MyModel
  228. def get_url_helper_class(self):
  229. if self.some_attribute is True:
  230. return MyURLHelper
  231. return AdminURLHelper
  232. .. _modeladmin_permission_helper_class:
  233. ``ModelAdmin.permission_helper_class``
  234. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  235. By default, the ``modeladmin.helpers.permission.PagePermissionHelper``
  236. class is used when your model extends ``wagtailcore.models.Page``,
  237. otherwise ``modeladmin.helpers.permission.PermissionHelper`` is used.
  238. If you find that the above helper classes don't work for your needs, you can
  239. easily create your own helper class, by sub-classing
  240. ``PermissionHelper`` (or ``PagePermissionHelper`` if your model extends Wagtail's ``Page`` model),
  241. and making any necessary additions/overrides. Once
  242. defined, you set the ``permission_helper_class`` attribute on your
  243. ``ModelAdmin`` class to use your custom class instead of the default, like so:
  244. .. code-block:: python
  245. from wagtail.contrib.modeladmin.helpers import PermissionHelper
  246. from wagtail.contrib.modeladmin.options import ModelAdmin, modeladmin_register
  247. from .models import MyModel
  248. class MyPermissionHelper(PermissionHelper):
  249. ...
  250. class MyModelAdmin(ModelAdmin):
  251. model = MyModel
  252. permission_helper_class = MyPermissionHelper
  253. modeladmin_register(MyModelAdmin)
  254. Or, if you have a more complicated use case, where simply setting an attribute
  255. isn't possible or doesn't meet your needs, you can override the
  256. ``get_permission_helper_class`` method, like so:
  257. .. code-block:: python
  258. class MyModelAdmin(ModelAdmin):
  259. model = MyModel
  260. def get_permission_helper_class(self):
  261. if self.some_attribute is True:
  262. return MyPermissionHelper
  263. return PermissionHelper
  264. .. _modeladmin_button_helper_class:
  265. ``ModelAdmin.button_helper_class``
  266. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  267. By default, the ``modeladmin.helpers.button.PageButtonHelper`` class is used
  268. when your model extends ``wagtailcore.models.Page``, otherwise
  269. ``modeladmin.helpers.button.ButtonHelper`` is used.
  270. If you wish to add or change buttons for your model's IndexView, you'll need to
  271. create your own button helper class by sub-classing ``ButtonHelper`` or ``PageButtonHelper`` (if
  272. your model extend's Wagtail's ``Page`` model), and
  273. make any necessary additions/overrides. Once defined, you set the
  274. ``button_helper_class`` attribute on your ``ModelAdmin`` class to use your
  275. custom class instead of the default, like so:
  276. .. code-block:: python
  277. from wagtail.contrib.modeladmin.helpers import ButtonHelper
  278. from wagtail.contrib.modeladmin.options import ModelAdmin, modeladmin_register
  279. from .models import MyModel
  280. class MyButtonHelper(ButtonHelper):
  281. def add_button(self, classnames_add=None, classnames_exclude=None):
  282. if classnames_add is None:
  283. classnames_add = []
  284. if classnames_exclude is None:
  285. classnames_exclude = []
  286. classnames = self.add_button_classnames + classnames_add
  287. cn = self.finalise_classname(classnames, classnames_exclude)
  288. return {
  289. 'url': self.url_helper.create_url,
  290. 'label': _('Add %s') % self.verbose_name,
  291. 'classname': cn,
  292. 'title': _('Add a new %s') % self.verbose_name,
  293. }
  294. def inspect_button(self, pk, classnames_add=None, classnames_exclude=None):
  295. ...
  296. def edit_button(self, pk, classnames_add=None, classnames_exclude=None):
  297. ...
  298. def delete_button(self, pk, classnames_add=None, classnames_exclude=None):
  299. ...
  300. class MyModelAdmin(ModelAdmin):
  301. model = MyModel
  302. button_helper_class = MyButtonHelper
  303. modeladmin_register(MyModelAdmin)
  304. To customise the buttons found in the ModelAdmin List View you can change the
  305. returned dictionary in the ``add_button``, ``delete_button``, ``edit_button``
  306. or ``inspect_button`` methods. For example if you wanted to change the ``Delete``
  307. button you could modify the ``delete_button`` method in your ``ButtonHelper`` like so:
  308. .. code-block:: python
  309. class MyButtonHelper(ButtonHelper):
  310. ...
  311. def delete_button(self, pk, classnames_add=None, classnames_exclude=None):
  312. ...
  313. return {
  314. 'url': reverse("your_custom_url"),
  315. 'label': _('Delete'),
  316. 'classname': "custom-css-class",
  317. 'title': _('Delete this item')
  318. }
  319. Or, if you have a more complicated use case, where simply setting an attribute
  320. isn't possible or doesn't meet your needs, you can override the
  321. ``get_button_helper_class`` method, like so:
  322. .. code-block:: python
  323. class MyModelAdmin(ModelAdmin):
  324. model = MyModel
  325. def get_button_helper_class(self):
  326. if self.some_attribute is True:
  327. return MyButtonHelper
  328. return ButtonHelper
  329. .. _modeladmin_helpers_in_custom_views:
  330. Using helpers in your custom views
  331. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  332. As long as you sub-class ``modeladmin.views.WMABaseView`` (or one of the more
  333. 'specific' view classes) to create your custom view, instances of each helper
  334. should be available on instances of your class as:
  335. - ``self.url_helper``
  336. - ``self.permission_helper``
  337. - ``self.button_helper``
  338. Unlike the other two, `self.button_helper` isn't populated right away when
  339. the view is instantiated. In order to show the right buttons for the right
  340. users, ButtonHelper instances need to be 'request aware', so
  341. ``self.button_helper`` is only set once the view's ``dispatch()`` method has
  342. run, which takes a ``HttpRequest`` object as an argument, from which the
  343. current user can be identified.