model_reference.rst 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. ===============
  2. Model Reference
  3. ===============
  4. .. automodule:: wagtail.core.models
  5. This document contains reference information for the model classes inside the ``wagtailcore`` module.
  6. .. _page-model-ref:
  7. ``Page``
  8. ========
  9. Database fields
  10. ~~~~~~~~~~~~~~~
  11. .. class:: Page
  12. .. attribute:: title
  13. (text)
  14. Human-readable title of the page.
  15. .. attribute:: draft_title
  16. (text)
  17. Human-readable title of the page, incorporating any changes that have been made in a draft edit (in contrast to the ``title`` field, which for published pages will be the title as it exists in the current published version).
  18. .. attribute:: slug
  19. (text)
  20. This is used for constructing the page's URL.
  21. For example: ``http://domain.com/blog/[my-slug]/``
  22. .. attribute:: content_type
  23. (foreign key to ``django.contrib.contenttypes.models.ContentType``)
  24. A foreign key to the :class:`~django.contrib.contenttypes.models.ContentType` object that represents the specific model of this page.
  25. .. attribute:: live
  26. (boolean)
  27. A boolean that is set to ``True`` if the page is published.
  28. Note: this field defaults to ``True`` meaning that any pages that are created programmatically will be published by default.
  29. .. attribute:: has_unpublished_changes
  30. (boolean)
  31. A boolean that is set to ``True`` when the page is either in draft or published with draft changes.
  32. .. attribute:: owner
  33. (foreign key to user model)
  34. A foreign key to the user that created the page.
  35. .. attribute:: first_published_at
  36. (date/time)
  37. The date/time when the page was first published.
  38. .. attribute:: last_published_at
  39. (date/time)
  40. The date/time when the page was last published.
  41. .. attribute:: seo_title
  42. (text)
  43. Alternate SEO-crafted title, for use in the page's ``<title>`` HTML tag.
  44. .. attribute:: search_description
  45. (text)
  46. SEO-crafted description of the content, used for search indexing. This is also suitable for the page's ``<meta name="description">`` HTML tag.
  47. .. attribute:: show_in_menus
  48. (boolean)
  49. Toggles whether the page should be included in site-wide menus.
  50. This is used by the :meth:`~wagtail.core.query.PageQuerySet.in_menu` QuerySet filter.
  51. Defaults to ``False`` and can be overridden on the model with ``show_in_menus_default = True``.
  52. .. note::
  53. To set the global default for all pages, set ``Page.show_in_menus_default = True`` once where you first import the ``Page`` model.
  54. Methods and properties
  55. ~~~~~~~~~~~~~~~~~~~~~~
  56. In addition to the model fields provided, ``Page`` has many properties and methods that you may wish to reference, use, or override in creating your own models.
  57. .. note::
  58. See also `django-treebeard <https://django-treebeard.readthedocs.io/en/latest/index.html>`_'s `node API <https://django-treebeard.readthedocs.io/en/latest/api.html>`_. ``Page`` is a subclass of `materialized path tree <https://django-treebeard.readthedocs.io/en/latest/mp_tree.html>`_ nodes.
  59. .. class:: Page
  60. .. autoattribute:: specific
  61. .. autoattribute:: specific_class
  62. .. automethod:: get_url
  63. .. autoattribute:: full_url
  64. .. automethod:: relative_url
  65. .. automethod:: get_site
  66. .. automethod:: get_url_parts
  67. .. automethod:: route
  68. .. automethod:: serve
  69. .. automethod:: get_context
  70. .. automethod:: get_template
  71. .. automethod:: get_admin_display_title
  72. .. autoattribute:: preview_modes
  73. .. automethod:: serve_preview
  74. .. automethod:: get_parent
  75. .. automethod:: get_ancestors
  76. .. automethod:: get_descendants
  77. .. automethod:: get_siblings
  78. .. attribute:: search_fields
  79. A list of fields to be indexed by the search engine. See Search docs :ref:`wagtailsearch_indexing_fields`
  80. .. attribute:: subpage_types
  81. A whitelist of page models which can be created as children of this page type. For example, a ``BlogIndex`` page might allow a ``BlogPage`` as a child, but not a ``JobPage``:
  82. .. code-block:: python
  83. class BlogIndex(Page):
  84. subpage_types = ['mysite.BlogPage', 'mysite.BlogArchivePage']
  85. The creation of child pages can be blocked altogether for a given page by setting it's subpage_types attribute to an empty array:
  86. .. code-block:: python
  87. class BlogPage(Page):
  88. subpage_types = []
  89. .. attribute:: parent_page_types
  90. A whitelist of page models which are allowed as parent page types. For example, a ``BlogPage`` may only allow itself to be created below the ``BlogIndex`` page:
  91. .. code-block:: python
  92. class BlogPage(Page):
  93. parent_page_types = ['mysite.BlogIndexPage']
  94. Pages can block themselves from being created at all by setting parent_page_types to an empty array (this is useful for creating unique pages that should only be created once):
  95. .. code-block:: python
  96. class HiddenPage(Page):
  97. parent_page_types = []
  98. .. automethod:: can_exist_under
  99. .. automethod:: can_create_at
  100. .. automethod:: can_move_to
  101. .. attribute:: password_required_template
  102. Defines which template file should be used to render the login form for Protected pages using this model. This overrides the default, defined using ``PASSWORD_REQUIRED_TEMPLATE`` in your settings. See :ref:`private_pages`
  103. .. attribute:: is_creatable
  104. Controls if this page can be created through the Wagtail administration. Defaults to ``True``, and is not inherited by subclasses. This is useful when using :ref:`multi-table inheritance <django:multi-table-inheritance>`, to stop the base model from being created as an actual page.
  105. .. attribute:: max_count
  106. Controls the maximum number of pages of this type that can be created through the Wagtail administration interface. This is useful when needing "allow at most 3 of these pages to exist", or for singleton pages.
  107. .. attribute:: max_count_per_parent
  108. Controls the maximum number of pages of this type that can be created under any one parent page.
  109. .. attribute:: exclude_fields_in_copy
  110. An array of field names that will not be included when a Page is copied.
  111. Useful when you have relations that do not use `ClusterableModel` or should not be copied.
  112. .. code-block:: python
  113. class BlogPage(Page):
  114. exclude_fields_in_copy = ['special_relation', 'custom_uuid']
  115. The following fields will always be excluded in a copy - `['id', 'path', 'depth', 'numchild', 'url_path', 'path']`.
  116. .. attribute:: base_form_class
  117. The form class used as a base for editing Pages of this type in the Wagtail page editor.
  118. This attribute can be set on a model to customise the Page editor form.
  119. Forms must be a subclass of :class:`~wagtail.admin.forms.WagtailAdminPageForm`.
  120. See :ref:`custom_edit_handler_forms` for more information.
  121. .. automethod:: with_content_json
  122. .. _site-model-ref:
  123. ``Site``
  124. ========
  125. The ``Site`` model is useful for multi-site installations as it allows an administrator to configure which part of the tree to use for each hostname that the server responds on.
  126. This configuration is used by the :class:`~wagtail.core.middleware.SiteMiddleware` middleware class which checks each request against this configuration and appends the Site object to the Django request object.
  127. Database fields
  128. ~~~~~~~~~~~~~~~
  129. .. class:: Site
  130. .. attribute:: hostname
  131. (text)
  132. This is the hostname of the site, excluding the scheme, port and path.
  133. For example: ``www.mysite.com``
  134. .. note::
  135. If you're looking for how to get the root url of a site, use the :attr:`~Site.root_url` attribute.
  136. .. attribute:: port
  137. (number)
  138. This is the port number that the site responds on.
  139. .. attribute:: site_name
  140. (text - optional)
  141. A human-readable name for the site. This is not used by Wagtail itself, but is suitable for use on the site front-end, such as in ``<title>`` elements.
  142. For example: ``Rod's World of Birds``
  143. .. attribute:: root_page
  144. (foreign key to :class:`~wagtail.core.models.Page`)
  145. This is a link to the root page of the site. This page will be what appears at the ``/`` URL on the site and would usually be a homepage.
  146. .. attribute:: is_default_site
  147. (boolean)
  148. This is set to ``True`` if the site is the default. Only one site can be the default.
  149. The default site is used as a fallback in situations where a site with the required hostname/port couldn't be found.
  150. Methods and properties
  151. ~~~~~~~~~~~~~~~~~~~~~~
  152. .. class:: Site
  153. .. automethod:: find_for_request
  154. .. autoattribute:: root_url
  155. This returns the URL of the site. It is calculated from the :attr:`~Site.hostname` and the :attr:`~Site.port` fields.
  156. The scheme part of the URL is calculated based on value of the :attr:`~Site.port` field:
  157. - 80 = ``http://``
  158. - 443 = ``https://``
  159. - Everything else will use the ``http://`` scheme and the port will be appended to the end of the hostname (eg. ``http://mysite.com:8000/``)
  160. .. automethod:: get_site_root_paths
  161. .. _page-revision-model-ref:
  162. ``PageRevision``
  163. ================
  164. Every time a page is edited a new ``PageRevision`` is created and saved to the database. It can be used to find the full history of all changes that have been made to a page and it also provides a place for new changes to be kept before going live.
  165. - Revisions can be created from any :class:`~wagtail.core.models.Page` object by calling its :meth:`~Page.save_revision` method
  166. - The content of the page is JSON-serialised and stored in the :attr:`~PageRevision.content_json` field
  167. - You can retrieve a ``PageRevision`` as a :class:`~wagtail.core.models.Page` object by calling the :meth:`~PageRevision.as_page_object` method
  168. Database fields
  169. ~~~~~~~~~~~~~~~
  170. .. class:: PageRevision
  171. .. attribute:: page
  172. (foreign key to :class:`~wagtail.core.models.Page`)
  173. .. attribute:: submitted_for_moderation
  174. (boolean)
  175. ``True`` if this revision is in moderation
  176. .. attribute:: created_at
  177. (date/time)
  178. This is the time the revision was created
  179. .. attribute:: user
  180. (foreign key to user model)
  181. This links to the user that created the revision
  182. .. attribute:: content_json
  183. (text)
  184. This field contains the JSON content for the page at the time the revision was created
  185. Managers
  186. ~~~~~~~~
  187. .. class:: PageRevision
  188. .. attribute:: objects
  189. This manager is used to retrieve all of the ``PageRevision`` objects in the database
  190. Example:
  191. .. code-block:: python
  192. PageRevision.objects.all()
  193. .. attribute:: submitted_revisions
  194. This manager is used to retrieve all of the ``PageRevision`` objects that are awaiting moderator approval
  195. Example:
  196. .. code-block:: python
  197. PageRevision.submitted_revisions.all()
  198. Methods and properties
  199. ~~~~~~~~~~~~~~~~~~~~~~
  200. .. class:: PageRevision
  201. .. automethod:: as_page_object
  202. This method retrieves this revision as an instance of its :class:`~wagtail.core.models.Page` subclass.
  203. .. automethod:: approve_moderation
  204. Calling this on a revision that's in moderation will mark it as approved and publish it
  205. .. automethod:: reject_moderation
  206. Calling this on a revision that's in moderation will mark it as rejected
  207. .. automethod:: is_latest_revision
  208. Returns ``True`` if this revision is its page's latest revision
  209. .. automethod:: publish
  210. Calling this will copy the content of this revision into the live page object. If the page is in draft, it will be published.
  211. ``GroupPagePermission``
  212. =======================
  213. Database fields
  214. ~~~~~~~~~~~~~~~
  215. .. class:: GroupPagePermission
  216. .. attribute:: group
  217. (foreign key to ``django.contrib.auth.models.Group``)
  218. .. attribute:: page
  219. (foreign key to :class:`~wagtail.core.models.Page`)
  220. .. attribute:: permission_type
  221. (choice list)
  222. ``PageViewRestriction``
  223. =======================
  224. Database fields
  225. ~~~~~~~~~~~~~~~
  226. .. class:: PageViewRestriction
  227. .. attribute:: page
  228. (foreign key to :class:`~wagtail.core.models.Page`)
  229. .. attribute:: password
  230. (text)
  231. ``Orderable`` (abstract)
  232. ========================
  233. Database fields
  234. ~~~~~~~~~~~~~~~
  235. .. class:: Orderable
  236. .. attribute:: sort_order
  237. (number)