Django Rest Framework:不清楚的错误消息 [英] Django Rest Framework: Unclear error message

查看:54
本文介绍了Django Rest Framework:不清楚的错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 views.py

class user_password(generics.UpdateAPIView):
    authentication_classes = ([JSONWebTokenAuthentication])
    serializer_class = user_password_serializer

    def get_queryset(self):
        return User.objects.get(id=self.request.user.id)

但是我在运行它时得到了它:

But I'm getting this when running it:


AssertionError:期望使用名为 pk的URL关键字参数调用视图user_password。修复您的URL conf,或在视图上正确设置 .lookup_field 属性。



<我知道序列化程序还可以,因为当我对同一事物使用不同类型的View时。它的工作原理是:

I know the serializer is okay because when I use a different type of View for the same thing. It works:

class user_password(APIView):

    authentication_classes = ([JSONWebTokenAuthentication])

    def put(self, request, format=None):

        serializer = user_password_serializer(data=request.data)
        if serializer.is_valid():
            if request.user.check_password(serializer.validated_data[
                                                'old_password']):
                request.user.set_password(serializer.validated_data[
                                          'new_password'])
                request.user.save()

                return Response({'success': True,
                                'result': serializer.validated_data},
                                status=status.HTTP_200_OK)
            else:
                    return Response({'success': False,
                                    'result': "credential mismatch"},
                                    status=status.HTTP_401_UNAUTHORIZED)


        return Response({'success': False,
                        'result': serializer.errors},
                        status=status.HTTP_400_BAD_REQUEST)

我不希望更改端点的构造方式。我确实有一个经过JWT验证的呼叫,我希望/ user / password能够将旧密码和新密码简单地输入到同一用户中。

I do not wish to change the way an endpoint is constructed. I do have a JWT authenticated call, and I wish /user/password would simply be able to PUT the 'old password' and 'new password' into that very same user.

我的 generics.UpdateAPIView 类在做什么?

推荐答案

<$ c是什么意思? .lookup_field $ c> .lookup_feild 是 UpdateAPIView 在您的url模式中期望的字段(捕获组)( pk 默认情况下),例如:

.lookup_feild is the field (capture group) the UpdateAPIView expects in your url pattern (pk by default), like:

r'^user/(?P<pk>\d+)/password/?$'

但是,您应该可以覆盖 get_object 而不是 get_queryset ,它应该可以在不对网址进行任何更改的情况下使用。

However, you should be able to override get_object instead of get_queryset and it should work without any changes to the url.

class user_password(generics.UpdateAPIView):
    authentication_classes = ([JSONWebTokenAuthentication])
    serializer_class = user_password_serializer

    def get_object(self):
        return User.objects.get(id=self.request.user.id)

此外,只需返回 self。 request.user 可以正常工作(我不知道您的项目中JWT身份验证的实现方式如何,但是大多数时候Django都会在 request.user 中拉出用户模型)。

Also, simply returning self.request.user will work just fine (I don't know how exactly JWT authentication implemented in your project, but most of the time Django pulls the user model in request.user anyway).

这篇关于Django Rest Framework:不清楚的错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆