您将如何保护DefaultRouter(),以便用户只有登录后才能查看API根目录?

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')),
]

project.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/

10-12 23:47