0

In this example wagtail is deployed to cms.example.com and is managing content for foo.example.com and bar.example.com as a headless CMS.

When accessing the Pages API on cms.example.com/api/pages/ only the pages for the current active site (cms.example.com) are returned.

To get the content for the "foo" and "bar" sites cms.example.com/api/ also needs to be available as foo.example.com/api/ and bar.example.com/api/ (e.g. using a proxy).

Is it possible to query the API for pages from other sites without having to go through some proxy?

jaap3
  • 2,696
  • 19
  • 34

1 Answers1

2

Wagtail's API is a fairly thin layer on top of Django Rest Framework, and you should feel encouraged to customise it or roll your own.

Filtering pages by the current site happens in the PagesAPIEndpoint.get_queryset method, and the simplest way of disabling that behaviour would be to subclass PagesAPIEndpoint to override that method, and register your custom subclass in urls.py in place of the original.

gasman
  • 23,691
  • 1
  • 38
  • 56
  • That would involve copying the entire body of `get_queryset`, something I'd like to avoid for maintainability. There's also the `child`/`descendant_of` filters, they are also restricted to the current site. It would be nice if I could somehow override `request.site`, other than accessing the site through a different hostname (e.g. some Param or header) – jaap3 May 25 '19 at 14:40
  • It seems much easier to write a custom `SiteMiddleware` that uses [`get_site_for_hostname`](https://github.com/wagtail/wagtail/blob/c9e740324c1a2197454274f5d18514b9a0752374/wagtail/core/sites.py#L10) to set the current site based on something other than the current hostname – jaap3 May 25 '19 at 15:06
  • In your case you don't have to copy the entire body of `get_queryset`. You have to replace it with `return Page.objects.all().public().live()` which afaik does what you need. – allcaps May 26 '19 at 19:13