Search code examples
djangowagtail

Wagtail: Can i use the API for fetching read-only drafts of pages for review?


The title pretty much sums up the question, the early docs talk about a /revision endpoint but i cannot find if it was ever implemented.

Wagtail have excellent functionality to edit and save the pages, i just need to preview how the draft looks when consumed by my application.


Solution

  • The API is designed to only serve the live version of pages, to avoid leaking information that isn't intended to be public. However, you can override this behaviour by subclassing PagesAPIEndpoint - for example:

    from django.http import Http404
    from rest_framework.response import Response
    from wagtail.api.v2.endpoints import PagesAPIEndpoint
    
    
    class DraftPagesAPIEndpoint(PagesAPIEndpoint):
        def detail_view(self, request, pk):
            instance = self.get_object()
    
            if request.GET.get('draft'):
                instance = instance.get_latest_revision_as_page()
            elif not instance.live:
                raise Http404
    
            serializer = self.get_serializer(instance)
            return Response(serializer.data)
    

    Then, when registering URL endpoints, use this class in place of PagesAPIEndpoint:

    api_router.register_endpoint('pages', DraftPagesAPIEndpoint)
    

    This will give you the ability to pass ?draft=true in the URL to get back a draft version.