【问题标题】:Rewriting DRF token auth重写 DRF 令牌认证
【发布时间】:2017-06-29 16:27:19
【问题描述】:

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

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

在我的设置中添加到REST_FRAMEWORK

'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'

和

ImportError: Could not import 'users.authentication.TokenAuthentication' for API setting 'DEFAULT_AUTHENTICATION_CLASSES'. ImportError: cannot import name 'APIView'.

有什么提示吗?

Token类修改是这样的:

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

【问题讨论】:

    标签: python django django-rest-framework django-rest-auth


    【解决方案1】:

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

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

    到另一个文件

    【讨论】:

      【解决方案2】:

      确保您已从 rest_framework.authentication 而不是从 rest_framework.authToken 导入 TokenAuthentication

      【讨论】:

        【解决方案3】:

        我一直在处理这个错误。事实证明,就像其他人所说的那样,它与代币有关。这并不完全是“修复”,但它为我消除了错误:

        我使用的是 simplejwt 并注释掉了导入: from rest_framework_simplejwt.tokens import RefreshToken 并且还使用 import 注释掉了函数。

        我的错误在此之后得到解决。

        【讨论】:

          猜你喜欢
          • 2022-11-04
          • 2017-10-29
          • 2021-08-28
          • 2015-12-07
          • 2020-04-10
          • 1970-01-01
          • 2021-05-25
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多