Django Rest Framework JWT-OperationalError:无此类表:auth_user

时间:2019-01-28 14:23:40

标签: django django-rest-framework jwt django-rest-framework-jwt

我正在使用Pyhton 2.7.15开发用于Windows的Django应用程序。我需要使用Django Rest framework JWT实现身份验证机制,这里我只需要验证令牌(而不生成令牌)。令牌的有效载荷是这样的:

有效载荷

{
  "iss": "customIssuer",
  "username": "customUser"
}

由于这个原因,我以这种方式配置setting.py

...

REST_FRAMEWORK = {
    'DEFAULT_PERMISSION_CLASSES': (
        'rest_framework.permissions.IsAuthenticated',
    ),
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework_jwt.authentication.JSONWebTokenAuthentication',
        'rest_framework.authentication.TokenAuthentication'
    ),
}

JWT_AUTH = {
    'JWT_ISSUER': 'customIssuer'
}

...

和我的urls.py以这种方式:

...

from rest_framework_jwt.views import verify_jwt_token

urlpatterns = [
    ...
    url(r'^api-token-verify/', verify_jwt_token),
    ...
]

然后,如果我尝试使用上述令牌发送POST请求,则会收到此错误:

Traceback (most recent call last):
  File "C:\Software\Workspace\py2_env\lib\site-packages\django\core\handlers\exception.py", line 41, in inner
    response = get_response(request)
  File "C:\Software\Workspace\py2_env\lib\site-packages\django\core\handlers\base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Software\Workspace\py2_env\lib\site-packages\django\core\handlers\base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Software\Workspace\py2_env\lib\site-packages\django\views\decorators\csrf.py", line 58, in wrapped_view
    return view_func(*args, **kwargs)
  File "C:\Software\Workspace\py2_env\lib\site-packages\django\views\generic\base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "C:\Software\Workspace\py2_env\lib\site-packages\rest_framework\views.py", line 489, in dispatch
    response = self.handle_exception(exc)
  File "C:\Software\Workspace\py2_env\lib\site-packages\rest_framework\views.py", line 449, in handle_exception
    self.raise_uncaught_exception(exc)
  File "C:\Software\Workspace\py2_env\lib\site-packages\rest_framework\views.py", line 486, in dispatch
    response = handler(request, *args, **kwargs)
  File "C:\Software\Workspace\py2_env\lib\site-packages\rest_framework_jwt\views.py", line 57, in post
    if serializer.is_valid():
  File "C:\Software\Workspace\py2_env\lib\site-packages\rest_framework\serializers.py", line 237, in is_valid
    self._validated_data = self.run_validation(self.initial_data)
  File "C:\Software\Workspace\py2_env\lib\site-packages\rest_framework\serializers.py", line 435, in run_validation
    value = self.validate(value)
  File "C:\Software\Workspace\py2_env\lib\site-packages\rest_framework_jwt\serializers.py", line 130, in validate
    user = self._check_user(payload=payload)
  File "C:\Software\Workspace\py2_env\lib\site-packages\rest_framework_jwt\serializers.py", line 109, in _check_user
    user = User.objects.get_by_natural_key(username)
  File "C:\Software\Workspace\py2_env\lib\site-packages\django\contrib\auth\base_user.py", line 48, in get_by_natural_key
    return self.get(**{self.model.USERNAME_FIELD: username})
  File "C:\Software\Workspace\py2_env\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "C:\Software\Workspace\py2_env\lib\site-packages\django\db\models\query.py", line 374, in get
    num = len(clone)
  File "C:\Software\Workspace\py2_env\lib\site-packages\django\db\models\query.py", line 232, in __len__
    self._fetch_all()
  File "C:\Software\Workspace\py2_env\lib\site-packages\django\db\models\query.py", line 1105, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "C:\Software\Workspace\py2_env\lib\site-packages\django\db\models\query.py", line 53, in __iter__
    results = compiler.execute_sql(chunked_fetch=self.chunked_fetch)
  File "C:\Software\Workspace\py2_env\lib\site-packages\django\db\models\sql\compiler.py", line 886, in execute_sql
    raise original_exception
OperationalError: no such table: auth_user

在检查“ rest framework jwt”包时,我注意到此错误是由serializers.py引发的。特别是通过check_user方法:

def _check_user(self, payload):

    username = jwt_get_username_from_payload(payload)

    if not username:
        msg = _('Invalid payload.')
        raise serializers.ValidationError(msg)

    # Make sure user exists
    try:
        user = User.objects.get_by_natural_key(username) ## ERROR!!!!
    except User.DoesNotExist:
        msg = _("User doesn't exist.")
        raise serializers.ValidationError(msg)

    if not user.is_active:
        msg = _('User account is disabled.')
        raise serializers.ValidationError(msg)

    return user

0 个答案:

没有答案