Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

django

wagtail

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.

like image 206
krs Avatar asked Oct 17 '25 18:10

krs


1 Answers

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.

like image 133
gasman Avatar answered Oct 20 '25 08:10

gasman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!