create_edit_delete_views.rst 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. ===========================================================
  2. Customising ``CreateView``, ``EditView`` and ``DeleteView``
  3. ===========================================================
  4. **NOTE:** ``modeladmin`` only provides 'create', 'edit' and 'delete'
  5. functionality for non page type models (i.e. models that do not extend
  6. ``wagtailcore.models.Page``). If your model is a 'page type' model, customising
  7. any of the following will not have any effect:
  8. .. _modeladmin_edit_handler_customisation:
  9. -------------------------------------------------------------
  10. Changing which fields appear in ``CreateView`` & ``EditView``
  11. -------------------------------------------------------------
  12. ``edit_handler`` can be used on any Django models.Model class, just like it
  13. can be used for ``Page`` models or other models registered as ``Snippets`` in
  14. Wagtail.
  15. To change the way your ``MyPageModel`` is displayed in the CreateView and the
  16. EditView, simply define an ``edit_handler`` or ``panels`` attribute on your
  17. model class.
  18. .. code-block:: python
  19. class MyPageModel(models.Model):
  20. first_name = models.CharField(max_length=100)
  21. last_name = models.CharField(max_length=100)
  22. address = models.TextField()
  23. panels = [
  24. MultiFieldPanel([
  25. FieldRowPanel([
  26. FieldPanel('first_name', classname='fn'),
  27. FieldPanel('last_name', classname='ln'),
  28. ]),
  29. FieldPanel('address', classname='custom1',)
  30. ])
  31. ]
  32. Or alternatively:
  33. .. code-block:: python
  34. class MyPageModel(models.Model):
  35. first_name = models.CharField(max_length=100)
  36. last_name = models.CharField(max_length=100)
  37. address = models.TextField()
  38. custom_panels = [
  39. MultiFieldPanel([
  40. FieldRowPanel([
  41. FieldPanel('first_name', classname='fn'),
  42. FieldPanel('last_name', classname='ln'),
  43. ]),
  44. FieldPanel('address', classname='custom1',)
  45. ])
  46. ]
  47. edit_handler = ObjectList(custom_panels)
  48. # or
  49. edit_handler = TabbedInterface([
  50. ObjectList(custom_panels, heading='First Tab'),
  51. ObjectList(...)
  52. ])
  53. ``edit_handler`` and ``panels`` can alternatively be
  54. defined on a ``ModelAdmin`` definition. This feature is especially useful
  55. for use cases where you have to work with models that are
  56. 'out of reach' (due to being part of a third-party package, for example).
  57. .. code-block:: python
  58. class BookAdmin(ModelAdmin):
  59. model = Book
  60. panels = [
  61. FieldPanel('title'),
  62. FieldPanel('author'),
  63. ]
  64. Or alternatively:
  65. .. code-block:: python
  66. class BookAdmin(ModelAdmin):
  67. model = Book
  68. custom_panels = [
  69. FieldPanel('title'),
  70. FieldPanel('author'),
  71. ]
  72. edit_handler = ObjectList(custom_panels)
  73. .. _modeladmin_form_view_extra_css:
  74. -----------------------------------
  75. ``ModelAdmin.form_view_extra_css``
  76. -----------------------------------
  77. **Expected value**: A list of path names of additional stylesheets to be added
  78. to ``CreateView`` and ``EditView``
  79. See the following part of the docs to find out more:
  80. :ref:`modeladmin_adding_css_and_js`
  81. .. _modeladmin_form_view_extra_js:
  82. -----------------------------------
  83. ``ModelAdmin.form_view_extra_js``
  84. -----------------------------------
  85. **Expected value**: A list of path names of additional js files to be added
  86. to ``CreateView`` and ``EditView``
  87. See the following part of the docs to find out more:
  88. :ref:`modeladmin_adding_css_and_js`
  89. .. _modeladmin_create_template_name:
  90. -----------------------------------
  91. ``ModelAdmin.create_template_name``
  92. -----------------------------------
  93. **Expected value**: The path to a custom template to use for ``CreateView``
  94. See the following part of the docs to find out more:
  95. :ref:`modeladmin_overriding_templates`
  96. .. _modeladmin_create_view_class:
  97. -----------------------------------
  98. ``ModelAdmin.create_view_class``
  99. -----------------------------------
  100. **Expected value**: A custom ``view`` class to replace
  101. ``modeladmin.views.CreateView``
  102. See the following part of the docs to find out more:
  103. :ref:`modeladmin_overriding_views`
  104. .. _modeladmin_edit_template_name:
  105. -----------------------------------
  106. ``ModelAdmin.edit_template_name``
  107. -----------------------------------
  108. **Expected value**: The path to a custom template to use for ``EditView``
  109. See the following part of the docs to find out more:
  110. :ref:`modeladmin_overriding_templates`
  111. .. _modeladmin_edit_view_class:
  112. -----------------------------------
  113. ``ModelAdmin.edit_view_class``
  114. -----------------------------------
  115. **Expected value**: A custom ``view`` class to replace
  116. ``modeladmin.views.EditView``
  117. See the following part of the docs to find out more:
  118. :ref:`modeladmin_overriding_views`
  119. .. _modeladmin_delete_template_name:
  120. -----------------------------------
  121. ``ModelAdmin.delete_template_name``
  122. -----------------------------------
  123. **Expected value**: The path to a custom template to use for ``DeleteView``
  124. See the following part of the docs to find out more:
  125. :ref:`modeladmin_overriding_templates`
  126. .. _modeladmin_delete_view_class:
  127. -----------------------------------
  128. ``ModelAdmin.delete_view_class``
  129. -----------------------------------
  130. **Expected value**: A custom ``view`` class to replace
  131. ``modeladmin.views.DeleteView``
  132. See the following part of the docs to find out more:
  133. :ref:`modeladmin_overriding_views`
  134. .. _modeladmin_form_fields_exclude:
  135. -----------------------------------
  136. ``ModelAdmin.form_fields_exclude``
  137. -----------------------------------
  138. **Expected value**: A list or tuple of fields names
  139. When using CreateView or EditView to create or update model instances, this
  140. value will be passed to the edit form, so that any named fields will be
  141. excluded from the form. This is particularly useful when registering ModelAdmin
  142. classes for models from third-party apps, where defining panel configurations
  143. on the Model itself is more complicated.
  144. .. _modeladmin_prepopulated_fields:
  145. -----------------------------------
  146. ``ModelAdmin.prepopulated_fields``
  147. -----------------------------------
  148. **Expected value**: A dict mapping prepopulated fields to a tuple of fields to
  149. prepopulate from
  150. When using CreateView or EditView to create or update model instances, the
  151. fields corresponding to the keys in the dict are prepopulated using the fields
  152. in the corresponding tuple. The main use for this functionality is to
  153. automatically generate the value for SlugField fields from one or more other
  154. fields. The generated value is produced by concatenating the values of the
  155. source fields, and then by transforming that result into a valid slug (e.g.
  156. substituting dashes for spaces; lowercasing ASCII letters; and removing various
  157. English stop words such as ‘a’, ‘an’, ‘as’, and similar).
  158. Prepopulated fields aren’t modified by JavaScript after a value has been saved.
  159. It’s usually undesired that slugs change (which would cause an object’s URL to
  160. change if the slug is used in it).
  161. prepopulated_fields doesn’t accept DateTimeField, ForeignKey, OneToOneField,
  162. and ManyToManyField fields.
  163. -----------------------------------
  164. ``ModelAdmin.get_edit_handler()``
  165. -----------------------------------
  166. **Must return**: An instance of ``wagtail.admin.edit_handlers.ObjectList``
  167. Returns the appropriate ``edit_handler`` for the modeladmin class.
  168. ``edit_handlers`` can be defined either on the model itself or on the
  169. modeladmin (as property ``edit_handler`` or ``panels``). Falls back to
  170. extracting panel / edit handler definitions from the model class.