2
0

index.rst 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. =====================
  2. ``ModelAdmin``
  3. =====================
  4. The ``modeladmin`` module allows you to create customisable listing
  5. pages for any model in your Wagtail project, and add navigation elements to the
  6. Wagtail admin area so that you can access them. Simply extend the
  7. ``ModelAdmin`` class, override a few attributes to suit your needs, register
  8. it with Wagtail using an easy one-line ``modeladmin_register`` method
  9. (you can copy and paste from the examples below), and you're good to go.
  10. You can use it with any Django model (it doesn’t need to extend ``Page`` or
  11. be registered as a ``Snippet``), and it won’t interfere with any of the
  12. existing admin functionality that Wagtail provides.
  13. .. _modeladmin_feature_summary:
  14. -------------------
  15. Summary of features
  16. -------------------
  17. - A customisable list view, allowing you to control what values are displayed
  18. for each row, available options for result filtering, default ordering, and
  19. more.
  20. - Access your list views from the Wagtail admin menu easily with automatically
  21. generated menu items, with automatic 'active item' highlighting. Control the
  22. label text and icons used with easy-to-change attributes on your class.
  23. - An additional ``ModelAdminGroup`` class, that allows you to group your
  24. related models, and list them together in their own submenu, for a more
  25. logical user experience.
  26. - Simple, robust **add** and **edit** views for your non-Page models that use
  27. the panel configurations defined on your model using Wagtail's edit panels.
  28. - For Page models, the system directs to Wagtail's existing add and
  29. edit views, and returns you back to the correct list page, for a seamless
  30. experience.
  31. - Full respect for permissions assigned to your Wagtail users and groups. Users
  32. will only be able to do what you want them to!
  33. - All you need to easily hook your ``ModelAdmin`` classes into Wagtail, taking
  34. care of URL registration, menu changes, and registering any missing model
  35. permissions, so that you can assign them to Groups.
  36. - **Built to be customisable** - While ``modeladmin`` provides a solid
  37. experience out of the box, you can easily use your own templates, and the
  38. ``ModelAdmin`` class has a large number of methods that you can override or
  39. extend, allowing you to customise the behaviour to a greater degree.
  40. ---------------------------------------------------
  41. Want to know more about customising ``ModelAdmin``?
  42. ---------------------------------------------------
  43. .. toctree::
  44. :maxdepth: 1
  45. primer
  46. menu_item
  47. indexview
  48. create_edit_delete_views
  49. inspectview
  50. chooseparentview
  51. .. _modeladmin_usage:
  52. Installation
  53. ------------
  54. Add ``wagtail.contrib.modeladmin`` to your ``INSTALLED_APPS``:
  55. .. code-block:: python
  56. INSTALLED_APPS = [
  57. ...
  58. 'wagtail.contrib.modeladmin',
  59. ]
  60. How to use
  61. ----------
  62. .. _modeladmin_example_simple:
  63. A simple example
  64. ^^^^^^^^^^^^^^^^
  65. You have a model in your app, and you want a listing page specifically for that
  66. model, with a menu item added to the menu in the Wagtail admin area so that you
  67. can get to it.
  68. ``wagtail_hooks.py`` in your app directory would look something like this:
  69. .. code-block:: python
  70. from wagtail.contrib.modeladmin.options import (
  71. ModelAdmin, modeladmin_register)
  72. from .models import MyPageModel
  73. class MyPageModelAdmin(ModelAdmin):
  74. model = MyPageModel
  75. menu_label = 'Page Model' # ditch this to use verbose_name_plural from model
  76. menu_icon = 'date' # change as required
  77. menu_order = 200 # will put in 3rd place (000 being 1st, 100 2nd)
  78. add_to_settings_menu = False # or True to add your model to the Settings sub-menu
  79. exclude_from_explorer = False # or True to exclude pages of this type from Wagtail's explorer view
  80. list_display = ('title', 'example_field2', 'example_field3', 'live')
  81. list_filter = ('live', 'example_field2', 'example_field3')
  82. search_fields = ('title',)
  83. # Now you just need to register your customised ModelAdmin class with Wagtail
  84. modeladmin_register(MyPageModelAdmin)
  85. .. _modeladmin_example_complex:
  86. A more complicated example
  87. ^^^^^^^^^^^^^^^^^^^^^^^^^^
  88. You have an app with several models that you want to show grouped together in
  89. Wagtail's admin menu. Some of the models might extend Page, and others might
  90. be simpler models, perhaps registered as Snippets, perhaps not. No problem!
  91. ModelAdminGroup allows you to group them all together nicely.
  92. ``wagtail_hooks.py`` in your app directory would look something like this:
  93. .. code-block:: python
  94. from wagtail.contrib.modeladmin.options import (
  95. ModelAdmin, ModelAdminGroup, modeladmin_register)
  96. from .models import (
  97. MyPageModel, MyOtherPageModel, MySnippetModel, SomeOtherModel)
  98. class MyPageModelAdmin(ModelAdmin):
  99. model = MyPageModel
  100. menu_label = 'Page Model' # ditch this to use verbose_name_plural from model
  101. menu_icon = 'doc-full-inverse' # change as required
  102. list_display = ('title', 'example_field2', 'example_field3', 'live')
  103. list_filter = ('live', 'example_field2', 'example_field3')
  104. search_fields = ('title',)
  105. class MyOtherPageModelAdmin(ModelAdmin):
  106. model = MyOtherPageModel
  107. menu_label = 'Other Page Model' # ditch this to use verbose_name_plural from model
  108. menu_icon = 'doc-full-inverse' # change as required
  109. list_display = ('title', 'example_field2', 'example_field3', 'live')
  110. list_filter = ('live', 'example_field2', 'example_field3')
  111. search_fields = ('title',)
  112. class MySnippetModelAdmin(ModelAdmin):
  113. model = MySnippetModel
  114. menu_label = 'Snippet Model' # ditch this to use verbose_name_plural from model
  115. menu_icon = 'snippet' # change as required
  116. list_display = ('title', 'example_field2', 'example_field3')
  117. list_filter = ('example_field2', 'example_field3')
  118. search_fields = ('title',)
  119. class SomeOtherModelAdmin(ModelAdmin):
  120. model = SomeOtherModel
  121. menu_label = 'Some other model' # ditch this to use verbose_name_plural from model
  122. menu_icon = 'snippet' # change as required
  123. list_display = ('title', 'example_field2', 'example_field3')
  124. list_filter = ('example_field2', 'example_field3')
  125. search_fields = ('title',)
  126. class MyModelAdminGroup(ModelAdminGroup):
  127. menu_label = 'My App'
  128. menu_icon = 'folder-open-inverse' # change as required
  129. menu_order = 200 # will put in 3rd place (000 being 1st, 100 2nd)
  130. items = (MyPageModelAdmin, MyOtherPageModelAdmin, MySnippetModelAdmin, SomeOtherModelAdmin)
  131. # When using a ModelAdminGroup class to group several ModelAdmin classes together,
  132. # you only need to register the ModelAdminGroup class with Wagtail:
  133. modeladmin_register(MyModelAdminGroup)
  134. .. _modeladmin_multi_registeration:
  135. Registering multiple classes in one ``wagtail_hooks.py`` file
  136. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  137. If you have an app with more than one model that you wish to manage, or even
  138. multiple models you wish to group together with ``ModelAdminGroup`` classes,
  139. that's possible. Just register each of your ModelAdmin classes using
  140. ``modeladmin_register``, and they'll work as expected.
  141. .. code-block:: python
  142. class MyPageModelAdmin(ModelAdmin):
  143. model = MyPageModel
  144. ...
  145. class MyOtherPageModelAdmin(ModelAdmin):
  146. model = MyOtherPageModel
  147. ...
  148. class MyModelAdminGroup(ModelAdminGroup):
  149. label = _("Group 1")
  150. items = (ModelAdmin1, ModelAdmin2)
  151. ...
  152. class MyOtherModelAdminGroup(ModelAdminGroup):
  153. label = _("Group 2")
  154. items = (ModelAdmin3, ModelAdmin4)
  155. ...
  156. modeladmin_register(MyPageModelAdmin)
  157. modeladmin_register(MyOtherPageModelAdmin)
  158. modeladmin_register(MyModelAdminGroup)
  159. modeladmin_register(MyOtherModelAdminGroup)