g:创建自定义API端点

时间:2018-08-06 17:46:09

标签: python django wagtail

我创建了一个名为“ Spotlights”的代码段,我想知道如何使用Wagtail API为代码段数据创建自定义端点。我最好的猜测是:

api_router.register_endpoint('Spotlights', BaseAPIEndpoint)

第一个arg是在建立端点的名称,还是引用某些东西?

2 个答案:

答案 0 :(得分:3)

我已经弄清楚了:只是Wagtail的BaseAPIEndpoint的子类。例如:

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)

此外,还有许多自定义方式。只需看一下Wagtail存储库中的endpoints.py文件:https://github.com/wagtail/wagtail/blob/master/wagtail/api/v2/endpoints.py

答案 1 :(得分:0)

根据Wagtail documentation,第一个参数是端点的名称(例如页面,图像),并且在端点的URL中使用该参数。 第二个参数是处理请求的端点类。

例如:

api_router.register_endpoint('pages', PagesAPIEndpoint)
api_router.register_endpoint('images', ImagesAPIEndpoint)
api_router.register_endpoint('documents', DocumentsAPIEndpoint)

所以,我建议你做个像

api_router.register_endpoint('spotlights', BaseAPIEndpoint)