Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 重写DRF令牌身份验证_Python_Django_Django Rest Framework_Django Rest Auth - Fatal编程技术网

Python 重写DRF令牌身份验证

Python 重写DRF令牌身份验证,python,django,django-rest-framework,django-rest-auth,Python,Django,Django Rest Framework,Django Rest Auth,我想允许用户拥有多个令牌,因此决定重写令牌模型。结果造成 class TokenAuthentication(rest_framework.authentication.TokenAuthentication): model = Token 在添加到REST\u框架的我的设置中 'DEFAULT_AUTHENTICATION_CLASSES': ( 'users.authentication.TokenAuthentication', ) 也修改 class ObtainAu

我想允许用户拥有多个令牌,因此决定重写令牌模型。结果造成

class TokenAuthentication(rest_framework.authentication.TokenAuthentication):
    model = Token
在添加到
REST\u框架的我的设置中

'DEFAULT_AUTHENTICATION_CLASSES': (
    'users.authentication.TokenAuthentication',
)
也修改

class ObtainAuthToken(APIView):
    authentication_classes = ()
    throttle_classes = ()
    permission_classes = ()
    parser_classes = (parsers.FormParser, parsers.MultiPartParser, parsers.JSONParser,)
    renderer_classes = (renderers.JSONRenderer,)
    serializer_class = AuthTokenSerializer


    def post(self, request, *args, **kwargs):
        serializer = self.serializer_class(data=request.data)
        serializer.is_valid(raise_exception=True)
        user = serializer.validated_data['user']
        name = serializer.validated_data['name']
        token, created = Token.objects.get_or_create(user=user, name=name)
        return Response({'token': token.key})


obtain_auth_token = ObtainAuthToken.as_view()
和AuthTokenSerializer返回名称

最后在URL中

url(r'^token-auth/', obtain_auth_token),
我认为一切都是正确的,但不断地犯错误

 File "/home/me/code/python/OCManager/core/users/authentication.py", line 4, in <module>
    from rest_framework.views import APIView
ImportError: cannot import name 'APIView'
有什么线索吗

令牌类修改如下:

class Token(rest_framework.authtoken.models.Token):
    # key is no longer primary key, but still indexed and unique
    key = models.CharField(_("Key"), max_length=40, db_index=True, unique=True)
    # relation to user is a ForeignKey, so each user can have more than one token
    user = models.ForeignKey(
        settings.AUTH_USER_MODEL, related_name='auth_tokens',
        on_delete=models.CASCADE, verbose_name=_("User")
    )
    name = models.CharField(_("Name"), max_length=64)

    class Meta:
        unique_together = (('user', 'name'),)

    def __str__(self):
        return self.user.username + " - " + self.name

我设法找到了问题所在。在同一个文件中使用TokenAuth扩展名导致了一些导入错误,似乎试图在其他文件中导入自身。解决方案正在移动

class TokenAuthentication(rest_framework.authentication.TokenAuthentication):
    model = Token

到另一个文件

确保您已从
rest\u framework.authentication
导入
TokenAuthentication
,而不是从
rest\u framework.authToken

class TokenAuthentication(rest_framework.authentication.TokenAuthentication):
    model = Token