我正在使用 Django 2.0
和 Django RESET Framework
来编写我的应用程序的 REST API
。
我已经配置了以下身份验证方法:
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.TokenAuthentication',
'rest_framework.authentication.BasicAuthentication',
'rest_framework.authentication.SessionAuthentication',
),
}
目前,所有经过身份验证的用户都可以访问web api
视图。
我想要的是允许少数用户(可能是超级管理员用户)通过会话认证或通过登录到Web浏览器来访问API。
编辑2:contacts/views.py
class ContactViewSet(viewsets.ModelViewSet):
queryset = Contact.objects.all()
serializer_class = ContactSerializer
permission_classes = (IsAuthenticated,)
def perform_create(self, serializer):
serializer.save(user_id=self.request.user)