gpt4 book ai didi

python - DRF : How to password protect the DefaultRouter

转载 作者:行者123 更新时间:2023-11-28 18:13:45 25 4
gpt4 key购买 nike

您将如何保护 DefaultRouter() 以便用户在登录前无法查看 API Root?

class OrderViewSet(viewsets.ReadOnlyModelViewSet):
permission_classes = (permissions.IsAuthenticated)
queryset = Order.objects.all()
serializer_class = OrderSerializer

router = routers.DefaultRouter()
router.register(r'orders', views.OrderViewSet)

urlpatterns = [
url(r'^', include(router.urls)),
url(r'^auth/', include('rest_framework.urls')),
]

项目.urls.py

# API
url(r'^api/', include(api_urls)),

最佳答案

您可以在 settings.py 文件中添加默认权限类:

REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.IsAuthenticated',
)
}

关于python - DRF : How to password protect the DefaultRouter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49772781/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com