index.rst 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. =====================
  2. ``ModelAdmin``
  3. =====================
  4. The ``modeladmin`` module allows you to add any model in your project to the Wagtail admin. You can create customisable listing
  5. 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.
  6. .. _modeladmin_feature_summary:
  7. -------------------
  8. Summary of features
  9. -------------------
  10. - A customisable list view, allowing you to control what values are displayed
  11. for each row, available options for result filtering, default ordering,
  12. spreadsheet downloads and more.
  13. - Access your list views from the Wagtail admin menu easily with automatically
  14. generated menu items, with automatic 'active item' highlighting. Control the
  15. label text and icons used with easy-to-change attributes on your class.
  16. - An additional ``ModelAdminGroup`` class, that allows you to group your
  17. related models, and list them together in their own submenu, for a more
  18. logical user experience.
  19. - Simple, robust **add** and **edit** views for your non-Page models that use
  20. the panel configurations defined on your model using Wagtail's edit panels.
  21. - For Page models, the system directs to Wagtail's existing add and
  22. edit views, and returns you back to the correct list page, for a seamless
  23. experience.
  24. - Full respect for permissions assigned to your Wagtail users and groups. Users
  25. will only be able to do what you want them to!
  26. - All you need to easily hook your ``ModelAdmin`` classes into Wagtail, taking
  27. care of URL registration, menu changes, and registering any missing model
  28. permissions, so that you can assign them to Groups.
  29. - **Built to be customisable** - While ``modeladmin`` provides a solid
  30. experience out of the box, you can easily use your own templates, and the
  31. ``ModelAdmin`` class has a large number of methods that you can override or
  32. extend, allowing you to customise the behaviour to a greater degree.
  33. ---------------------------------------------------
  34. Want to know more about customising ``ModelAdmin``?
  35. ---------------------------------------------------
  36. .. toctree::
  37. :maxdepth: 1
  38. primer
  39. menu_item
  40. indexview
  41. create_edit_delete_views
  42. inspectview
  43. chooseparentview
  44. tips_and_tricks/index
  45. .. _modeladmin_usage:
  46. Installation
  47. ------------
  48. Add ``wagtail.contrib.modeladmin`` to your ``INSTALLED_APPS``:
  49. .. code-block:: python
  50. INSTALLED_APPS = [
  51. ...
  52. 'wagtail.contrib.modeladmin',
  53. ]
  54. How to use
  55. ----------
  56. .. _modeladmin_example_simple:
  57. A simple example
  58. ^^^^^^^^^^^^^^^^
  59. Let's say your website is for a local library. They have a model called
  60. ``Book`` that appears across the site in many places. You can define a normal
  61. Django model for it, then use ModelAdmin to create a menu in Wagtail's admin
  62. to create, view, and edit ``Book`` entries.
  63. ``models.py`` looks like this:
  64. .. code-block:: python
  65. from django.db import models
  66. from wagtail.admin.edit_handlers import FieldPanel
  67. from wagtail.images.edit_handlers import ImageChooserPanel
  68. class Book(models.Model):
  69. title = models.CharField(max_length=255)
  70. author = models.CharField(max_length=255)
  71. cover_photo = models.ForeignKey(
  72. 'wagtailimages.Image',
  73. null=True, blank=True,
  74. on_delete=models.SET_NULL,
  75. related_name='+'
  76. )
  77. panels = [
  78. FieldPanel('title'),
  79. FieldPanel('author'),
  80. ImageChooserPanel('cover_photo')
  81. ]
  82. .. tip::
  83. You can specify ``FieldPanels`` like ``ImageChooserPanel``, ``PageChooserPanel``,
  84. and ``DocumentChooserPanel`` within the ``panels`` attribute of the model.
  85. This lets you use Wagtail-specific features in an otherwise traditional
  86. Django model.
  87. ``wagtail_hooks.py`` in your app directory would look something like this:
  88. .. code-block:: python
  89. from wagtail.contrib.modeladmin.options import (
  90. ModelAdmin, modeladmin_register)
  91. from .models import Book
  92. class BookAdmin(ModelAdmin):
  93. model = Book
  94. menu_label = 'Book' # ditch this to use verbose_name_plural from model
  95. menu_icon = 'pilcrow' # change as required
  96. menu_order = 200 # will put in 3rd place (000 being 1st, 100 2nd)
  97. add_to_settings_menu = False # or True to add your model to the Settings sub-menu
  98. exclude_from_explorer = False # or True to exclude pages of this type from Wagtail's explorer view
  99. list_display = ('title', 'author')
  100. list_filter = ('author',)
  101. search_fields = ('title', 'author')
  102. # Now you just need to register your customised ModelAdmin class with Wagtail
  103. modeladmin_register(BookAdmin)
  104. .. _modeladmin_example_complex:
  105. A more complicated example
  106. ^^^^^^^^^^^^^^^^^^^^^^^^^^
  107. In addition to ``Book``, perhaps we also want to add ``Author`` and ``Genre``
  108. models to our app and display a menu item for each of them, too. Creating
  109. lots of menus can add up quickly, so it might be a good idea to group related
  110. menus together. This section show you how to create one menu called *Library*
  111. which expands to show submenus for *Book*, *Author*, and *Genre*.
  112. Assume we've defined ``Book``, ``Author``, and ``Genre`` models in
  113. ``models.py``.
  114. ``wagtail_hooks.py`` in your app directory would look something like this:
  115. .. code-block:: python
  116. from wagtail.contrib.modeladmin.options import (
  117. ModelAdmin, ModelAdminGroup, modeladmin_register)
  118. from .models import (
  119. Book, Author, Genre)
  120. class BookAdmin(ModelAdmin):
  121. model = Book
  122. menu_label = 'Book' # ditch this to use verbose_name_plural from model
  123. menu_icon = 'pilcrow' # change as required
  124. list_display = ('title', 'author')
  125. list_filter = ('genre', 'author')
  126. search_fields = ('title', 'author')
  127. class AuthorAdmin(ModelAdmin):
  128. model = Author
  129. menu_label = 'Author' # ditch this to use verbose_name_plural from model
  130. menu_icon = 'user' # change as required
  131. list_display = ('first_name', 'last_name')
  132. list_filter = ('first_name', 'last_name')
  133. search_fields = ('first_name', 'last_name')
  134. class GenreAdmin(ModelAdmin):
  135. model = Genre
  136. menu_label = 'Genre' # ditch this to use verbose_name_plural from model
  137. menu_icon = 'group' # change as required
  138. list_display = ('name',)
  139. list_filter = ('name',)
  140. search_fields = ('name',)
  141. class LibraryGroup(ModelAdminGroup):
  142. menu_label = 'Library'
  143. menu_icon = 'folder-open-inverse' # change as required
  144. menu_order = 200 # will put in 3rd place (000 being 1st, 100 2nd)
  145. items = (BookAdmin, AuthorAdmin, GenreAdmin)
  146. # When using a ModelAdminGroup class to group several ModelAdmin classes together,
  147. # you only need to register the ModelAdminGroup class with Wagtail:
  148. modeladmin_register(LibraryGroup)
  149. .. _modeladmin_multi_registration:
  150. Registering multiple classes in one ``wagtail_hooks.py`` file
  151. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  152. Each time you call ``modeladmin_register(MyAdmin)`` it creates a new top-level
  153. menu item in Wagtail's left sidebar. You can call this multiple times within
  154. the same ``wagtail_hooks.py`` file if you want. The example below will create
  155. 3 top-level menus.
  156. .. code-block:: python
  157. class BookAdmin(ModelAdmin):
  158. model = Book
  159. ...
  160. class MovieAdmin(ModelAdmin):
  161. model = MovieModel
  162. ...
  163. class MusicAdminGroup(ModelAdminGroup):
  164. menu_label = _("Music")
  165. items = (AlbumAdmin, ArtistAdmin)
  166. ...
  167. modeladmin_register(BookAdmin)
  168. modeladmin_register(MovieAdmin)
  169. modeladmin_register(MusicAdminGroup)