Wagtail:创建自定义API端点

2024-04-19 04:30:29 发布

您现在位置:Python中文网/ 问答频道 /正文

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

api_router.register_endpoint('Spotlights', BaseAPIEndpoint)

第一个参数是建立端点的名称,还是引用了什么?在


Tags: 数据名称registerapi参数代码段端点endpoint
2条回答

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

例如:

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

所以,我建议你:

^{pr2}$

我已经弄明白了:只需将Wagtail的BaseAPIEndpoint子类。例如:

端点.py

from wagtail.api.v2.endpoints import BaseAPIEndpoint

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

api.py文件

^{pr2}$

另外还有很多方法可以定制它。看看端点.pyWagtail repo中的文件:https://github.com/wagtail/wagtail/blob/master/wagtail/api/v2/endpoints.py

相关问题 更多 >