我試圖讓當前的用戶模型中的串行self.context [「請求」]。用戶總是返回AnonymousUser
self.context['request'].user
但它總是返回AnonymousUser。 這裏是串行代碼片斷
class QuestionBlockDetailSerializer(serializers.ModelSerializer):
isvoted = serializers.SerializerMethodField(read_only=True)
def get_isvoted(self,obj):
user = self.context['request'].user
print self.context['request'].user
...
下面是設置權限和AuthBackends
AUTHENTICATION_BACKENDS = (
'django.contrib.auth.backends.ModelBackend',
'social.backends.google.GoogleOAuth2',
'social.backends.facebook.FacebookOAuth2',)
'DEFAULT_AUTHENTICATION_CLASSES': (
'oauth2_provider.ext.rest_framework.OAuth2Authentication',
'rest_framework_social_oauth2.authentication.SocialAuthentication',
'rest_framework.authentication.TokenAuthentication',
),
觀點:
class QuestionBlockDetailAPI(generics.RetrieveAPIView, mixins.UpdateModelMixin, mixins.DestroyModelMixin):
permission_classes =[IsAuthenticatedOrReadOnly]
serializer_class = QuestionBlockDetailSerializer
queryset= Question_Block.objects.all()
lookup_field = 'q_slug'
def delete(self, request, *args, **kwargs):
return self.destroy(request,*args, **kwargs)
def put(self, request, *args, **kwargs):
return self.update(request,*args,**kwargs)
我嘗試添加的會話和BasicAuthentication但它總是返回AnonymousUser。請幫助我。
你能證明你的觀點:
你可以通過改變你
def get_isvoted(self,obj):
方法類似解決這個問題? –