Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wagtail: Creating a custom API endpoint

I've created a Snippet called "Spotlights," and I'm wondering how I can create a custom endpoint for Snippet data with the Wagtail API. My best guess would be:

api_router.register_endpoint('Spotlights', BaseAPIEndpoint)

Is the first arg there establishing the name of the endpoint, or referencing something?

like image 963
LazerFriends Avatar asked Jan 02 '23 01:01

LazerFriends


1 Answers

I've figured it out: just subclass Wagtail's BaseAPIEndpoint. For example:

endpoints.py

from wagtail.api.v2.endpoints import BaseAPIEndpoint

class SpotlightsAPIEndpoint(BaseAPIEndpoint):
    ...
    model = Spotlight

api.py

from .endpoints import SpotlightsAPIEndpoint

api_router.register_endpoint('spotlights', SpotlightsAPIEndpoint)

Plus there are tons of ways to customize it. Just take a look at the endpoints.py file in the Wagtail repo: https://github.com/wagtail/wagtail/blob/master/wagtail/api/v2/endpoints.py

like image 118
LazerFriends Avatar answered Jan 05 '23 15:01

LazerFriends