Wagtail:创建自定义 API 端点
Wagtail: Creating a custom API endpoint
我创建了一个名为 "Spotlights," 的代码段,我想知道如何使用 Wagtail API 为代码段数据创建自定义端点。我最好的猜测是:
api_router.register_endpoint('Spotlights', BaseAPIEndpoint)
那里的第一个参数是建立端点的名称,还是在引用某些东西?
根据Wagtail documentation,第一个参数是端点的名称(例如页面、图像),这在端点的URL 中使用。
第二个参数是处理请求的端点class。
例如:
api_router.register_endpoint('pages', PagesAPIEndpoint)
api_router.register_endpoint('images', ImagesAPIEndpoint)
api_router.register_endpoint('documents', DocumentsAPIEndpoint)
所以,我建议你像这样:
api_router.register_endpoint('spotlights', BaseAPIEndpoint)
我已经弄明白了:只需继承 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
最新的 Wagtail 版本 - 2.15 +
在您的视图文件中,导入您的模型和 BaseApiViewSet
from .models import CustomModel
from wagtail.api.v2.views import BaseAPIViewSet
class BusinessLocationViewSet(BaseAPIViewSet):
model = BusinessLocation
body_fields = BaseAPIViewSet.body_fields + ['id', 'field1', 'field2', 'field3', 'field4', etc, etc...]
并在项目文件夹的 api.py 文件中,导入您的模型并将其暴露给 api,如下所示:
from custommodel.views import MyModel
api_router.register_endpoint('custom', MyModel)
现在您的模型可以从 api/v2/custom
端点访问
我需要稍微修改 glls 的解决方案以使其工作。在 api.py
中公开端点:
from .views import CustomViewSet
api_router.register_endpoint("custom", CustomViewSet)
添加过滤功能也很常见。在 views.py
:
from wagtail.images.api.v2.views import BaseAPIViewSet, FieldsFilter
from .models import Custom
class CustomViewSet(BaseAPIViewSet):
model = Custom
body_fields = BaseAPIViewSet.body_fields + [
"name"
]
filter_backends = [
FieldsFilter
]
您现在可以像往常一样过滤 Custom
片段:/api/v2/custom/?name=something
我创建了一个名为 "Spotlights," 的代码段,我想知道如何使用 Wagtail API 为代码段数据创建自定义端点。我最好的猜测是:
api_router.register_endpoint('Spotlights', BaseAPIEndpoint)
那里的第一个参数是建立端点的名称,还是在引用某些东西?
根据Wagtail documentation,第一个参数是端点的名称(例如页面、图像),这在端点的URL 中使用。 第二个参数是处理请求的端点class。
例如:
api_router.register_endpoint('pages', PagesAPIEndpoint)
api_router.register_endpoint('images', ImagesAPIEndpoint)
api_router.register_endpoint('documents', DocumentsAPIEndpoint)
所以,我建议你像这样:
api_router.register_endpoint('spotlights', BaseAPIEndpoint)
我已经弄明白了:只需继承 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
最新的 Wagtail 版本 - 2.15 +
在您的视图文件中,导入您的模型和 BaseApiViewSet
from .models import CustomModel
from wagtail.api.v2.views import BaseAPIViewSet
class BusinessLocationViewSet(BaseAPIViewSet):
model = BusinessLocation
body_fields = BaseAPIViewSet.body_fields + ['id', 'field1', 'field2', 'field3', 'field4', etc, etc...]
并在项目文件夹的 api.py 文件中,导入您的模型并将其暴露给 api,如下所示:
from custommodel.views import MyModel
api_router.register_endpoint('custom', MyModel)
现在您的模型可以从 api/v2/custom
端点访问
我需要稍微修改 glls 的解决方案以使其工作。在 api.py
中公开端点:
from .views import CustomViewSet
api_router.register_endpoint("custom", CustomViewSet)
添加过滤功能也很常见。在 views.py
:
from wagtail.images.api.v2.views import BaseAPIViewSet, FieldsFilter
from .models import Custom
class CustomViewSet(BaseAPIViewSet):
model = Custom
body_fields = BaseAPIViewSet.body_fields + [
"name"
]
filter_backends = [
FieldsFilter
]
您现在可以像往常一样过滤 Custom
片段:/api/v2/custom/?name=something