如何从vue.js中的请求获得DRF中的self.request.user ?
我对.vue
的请求:
async LoadCase() {
this.case = await fetch(
`${this.$store.getters.getServerUrl}/casedetail/${this.slug}`
).then(response => response.json())
}
我在views.py
的课:
class DetailCase(RetrieveAPIView):
queryset = Case.objects.all()
serializer_class = CaseDetalSerializers
lookup_field = "slug"
My class inserializers.py
:
class CaseDetalSerializers(serializers.ModelSerializer):
possible_open = serializers.SerializerMethodField()
class Meta:
model = Case
exclude = ("id","slug")
def get_possible_open(self,case):
request = self.context.get('request')
print(request)
print(request.user)
我从print
得到的:
<rest_framework.request.Request: GET '/api/v1/casedetail/rofl'>
AnonymousUser
现在因为某些原因我得到了AnonymousUser,我应该得到admin。我要怎么做才能得到它?
你好,你应该为你的API使用身份验证方法,以便获得一个用户,否则它将是一个AnonymousUser对象。
这是jwt身份验证的一个简单示例https://simpleisbetterthancomplex.com/tutorial/2018/12/19/how-to-use-jwt-authentication-with-django-rest-framework.html