2
0

configuration.rst 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. .. _api_v2_configuration:
  2. ==================================
  3. Wagtail API v2 Configuration Guide
  4. ==================================
  5. This section of the docs will show you how to set up a public API for your
  6. Wagtail site.
  7. Even though the API is built on Django REST Framework, you do not need to
  8. install this manually as it is already a dependency of Wagtail.
  9. Basic configuration
  10. ===================
  11. Enable the app
  12. --------------
  13. Firstly, you need to enable Wagtail's API app so Django can see it.
  14. Add ``wagtail.api.v2`` to ``INSTALLED_APPS`` in your Django project settings:
  15. .. code-block:: python
  16. # settings.py
  17. INSTALLED_APPS = [
  18. ...
  19. 'wagtail.api.v2',
  20. ...
  21. ]
  22. Optionally, you may also want to add ``rest_framework`` to ``INSTALLED_APPS``.
  23. This would make the API browsable when viewed from a web browser but is not
  24. required for basic JSON-formatted output.
  25. Configure endpoints
  26. -------------------
  27. Next, it's time to configure which content will be exposed on the API. Each
  28. content type (such as pages, images and documents) has its own endpoint.
  29. Endpoints are combined by a router, which provides the url configuration you
  30. can hook into the rest of your project.
  31. Wagtail provides three endpoint classes you can use:
  32. - Pages :class:`wagtail.api.v2.views.PagesAPIViewSet`
  33. - Images :class:`wagtail.images.api.v2.views.ImagesAPIViewSet`
  34. - Documents :class:`wagtail.documents.api.v2.views.DocumentsAPIViewSet`
  35. You can subclass any of these endpoint classes to customise their functionality.
  36. Additionally, there is a base endpoint class you can use for adding different
  37. content types to the API: :class:`wagtail.api.v2.views.BaseAPIViewSet`
  38. For this example, we will create an API that includes all three builtin content
  39. types in their default configuration:
  40. .. code-block:: python
  41. # api.py
  42. from wagtail.api.v2.views import PagesAPIViewSet
  43. from wagtail.api.v2.router import WagtailAPIRouter
  44. from wagtail.images.api.v2.views import ImagesAPIViewSet
  45. from wagtail.documents.api.v2.views import DocumentsAPIViewSet
  46. # Create the router. "wagtailapi" is the URL namespace
  47. api_router = WagtailAPIRouter('wagtailapi')
  48. # Add the three endpoints using the "register_endpoint" method.
  49. # The first parameter is the name of the endpoint (eg. pages, images). This
  50. # is used in the URL of the endpoint
  51. # The second parameter is the endpoint class that handles the requests
  52. api_router.register_endpoint('pages', PagesAPIViewSet)
  53. api_router.register_endpoint('images', ImagesAPIViewSet)
  54. api_router.register_endpoint('documents', DocumentsAPIViewSet)
  55. Next, register the URLs so Django can route requests into the API:
  56. .. code-block:: python
  57. # urls.py
  58. from .api import api_router
  59. urlpatterns = [
  60. ...
  61. path('api/v2/', api_router.urls),
  62. ...
  63. # Ensure that the api_router line appears above the default Wagtail page serving route
  64. re_path(r'^', include(wagtail_urls)),
  65. ]
  66. With this configuration, pages will be available at ``/api/v2/pages/``, images
  67. at ``/api/v2/images/`` and documents at ``/api/v2/documents/``
  68. .. _apiv2_page_fields_configuration:
  69. Adding custom page fields
  70. -------------------------
  71. It's likely that you would need to export some custom fields over the API. This
  72. can be done by adding a list of fields to be exported into the ``api_fields``
  73. attribute for each page model.
  74. For example:
  75. .. code-block:: python
  76. # blog/models.py
  77. from wagtail.api import APIField
  78. class BlogPageAuthor(Orderable):
  79. page = models.ForeignKey('blog.BlogPage', on_delete=models.CASCADE, related_name='authors')
  80. name = models.CharField(max_length=255)
  81. api_fields = [
  82. APIField('name'),
  83. ]
  84. class BlogPage(Page):
  85. published_date = models.DateTimeField()
  86. body = RichTextField()
  87. feed_image = models.ForeignKey('wagtailimages.Image', on_delete=models.SET_NULL, null=True, ...)
  88. private_field = models.CharField(max_length=255)
  89. # Export fields over the API
  90. api_fields = [
  91. APIField('published_date'),
  92. APIField('body'),
  93. APIField('feed_image'),
  94. APIField('authors'), # This will nest the relevant BlogPageAuthor objects in the API response
  95. ]
  96. This will make ``published_date``, ``body``, ``feed_image`` and a list of
  97. ``authors`` with the ``name`` field available in the API. But to access these
  98. fields, you must select the ``blog.BlogPage`` type using the ``?type``
  99. :ref:`parameter in the API itself <apiv2_custom_page_fields>`.
  100. Custom serialisers
  101. ------------------
  102. Serialisers_ are used to convert the database representation of a model into
  103. JSON format. You can override the serialiser for any field using the
  104. ``serializer`` keyword argument:
  105. .. code-block:: python
  106. from rest_framework.fields import DateField
  107. class BlogPage(Page):
  108. ...
  109. api_fields = [
  110. # Change the format of the published_date field to "Thursday 06 April 2017"
  111. APIField('published_date', serializer=DateField(format='%A %d %B %Y')),
  112. ...
  113. ]
  114. Django REST framework's serializers can all take a source_ argument allowing you
  115. to add API fields that have a different field name or no underlying field at all:
  116. .. code-block:: python
  117. from rest_framework.fields import DateField
  118. class BlogPage(Page):
  119. ...
  120. api_fields = [
  121. # Date in ISO8601 format (the default)
  122. APIField('published_date'),
  123. # A separate published_date_display field with a different format
  124. APIField('published_date_display', serializer=DateField(format='%A $d %B %Y', source='published_date')),
  125. ...
  126. ]
  127. This adds two fields to the API (other fields omitted for brevity):
  128. .. code-block:: json
  129. {
  130. "published_date": "2017-04-06",
  131. "published_date_display": "Thursday 06 April 2017"
  132. }
  133. .. _Serialisers: https://www.django-rest-framework.org/api-guide/fields/
  134. .. _source: https://www.django-rest-framework.org/api-guide/fields/#source
  135. Images in the API
  136. -----------------
  137. The :class:`~wagtail.images.api.fields.ImageRenditionField` serialiser
  138. allows you to add renditions of images into your API. It requires an image
  139. filter string specifying the resize operations to perform on the image. It can
  140. also take the ``source`` keyword argument described above.
  141. For example:
  142. .. code-block:: python
  143. from wagtail.images.api.fields import ImageRenditionField
  144. class BlogPage(Page):
  145. ...
  146. api_fields = [
  147. # Adds information about the source image (eg, title) into the API
  148. APIField('feed_image'),
  149. # Adds a URL to a rendered thumbnail of the image to the API
  150. APIField('feed_image_thumbnail', serializer=ImageRenditionField('fill-100x100', source='feed_image')),
  151. ...
  152. ]
  153. This would add the following to the JSON:
  154. .. code-block:: json
  155. {
  156. "feed_image": {
  157. "id": 45529,
  158. "meta": {
  159. "type": "wagtailimages.Image",
  160. "detail_url": "http://www.example.com/api/v2/images/12/",
  161. "download_url": "/media/images/a_test_image.jpg",
  162. "tags": []
  163. },
  164. "title": "A test image",
  165. "width": 2000,
  166. "height": 1125
  167. },
  168. "feed_image_thumbnail": {
  169. "url": "/media/images/a_test_image.fill-100x100.jpg",
  170. "width": 100,
  171. "height": 100,
  172. "alt": "image alt text"
  173. }
  174. }
  175. Note: ``download_url`` is the original uploaded file path, whereas
  176. ``feed_image_thumbnail['url']`` is the url of the rendered image.
  177. When you are using another storage backend, such as S3, ``download_url`` will return
  178. a URL to the image if your media files are properly configured.
  179. Additional settings
  180. ===================
  181. ``WAGTAILAPI_BASE_URL``
  182. -----------------------
  183. (required when using frontend cache invalidation)
  184. This is used in two places, when generating absolute URLs to document files and
  185. invalidating the cache.
  186. Generating URLs to documents will fall back the the current request's hostname
  187. if this is not set. Cache invalidation cannot do this, however, so this setting
  188. must be set when using this module alongside the ``wagtailfrontendcache`` module.
  189. ``WAGTAILAPI_SEARCH_ENABLED``
  190. -----------------------------
  191. (default: True)
  192. Setting this to false will disable full text search. This applies to all
  193. endpoints.
  194. ``WAGTAILAPI_LIMIT_MAX``
  195. ------------------------
  196. (default: 20)
  197. This allows you to change the maximum number of results a user can request at a
  198. time. This applies to all endpoints. Set to ``None`` for no limit.