【发布时间】:2017-01-24 15:20:53
【问题描述】:
我知道这个问题之前已经回答过,但答案并没有解决我的困惑。
所以我有一个视图可以检查用户的用户名和密码是否正确-
@api_view(['POST'])
def example_view(request, format=None):
username = request.data.get("username")
password = request.data.get("password")
content = {}
try:
user = User.objects.get(username=username)
if user.check_password(password):
content = {'user': unicode(user.username),'token': unicode(user.auth_token),}
else:
content = {'wrong password'}
except User.DoesNotExist:
content = {
'not registered',
}
return Response(content)
如果用户名和密码正确,我会发出 post 请求并返回令牌。
现在我做了一个模型-
class Check(models.Model):
owner = models.ForeignKey(User,verbose_name = 'UserName')
mood = models.TextField(null=True)
而且我有相同型号的功能-
@api_view(['GET'])
def CheckApi(request):
authentication_classes = (TokenAuthentication,)
permission_classes = (IsAuthenticated,)
?? query_set = Check.objects.get(owner=
serializer = CheckSerializer(qs, many=True)
return Response(serializer.data)
还有它的序列化器-
class CheckSerializer(serializers.ModelSerializer):
class Meta:
model = Check
fields = ('__all__')
我基本上想要做的是在用户经过身份验证之后,我希望 api 为 那个 用户返回数据,而不是为所有用户返回数据.
【问题讨论】:
标签: django authentication django-rest-framework django-rest-auth