在Django中将JWT与自定义用户模型一起使用时遇到问题

时间:2018-07-19 23:04:00

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

我已经开始了一个新的Django项目,并且像文档中所建议的那样,我已经创建了一个自定义用户模型,以防万一,并确保在设置中设置AUTH_USER_MODEL

from django.contrib.auth.models import AbstractUser

class User(AbstractUser):
    pass

我注意到,执行此操作无法运行python manage.py makemigrations,实际上必须指定python manage.py makemigrations <appname>中的自定义用户模型所使用的应用程序。完成后,就可以启动服务器了。

我关注了这篇文章,将JWT集成到我的新项目中:Django React JWT

在没有自定义用户模型的情况下,帖子的效果很好,但是一旦添加自定义用户模型,我就会开始出现以下错误:

Traceback (most recent call last):
  File "/Users/.local/share/virtualenvs/api-RqAznZCl/lib/python3.7/site-packages/django/core/handlers/exception.py", line 35, in inner
    response = get_response(request)
  File "/Users/.local/share/virtualenvs/api-RqAznZCl/lib/python3.7/site-packages/django/core/handlers/base.py", line 128, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/Users/.local/share/virtualenvs/api-RqAznZCl/lib/python3.7/site-packages/django/core/handlers/base.py", line 126, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/.local/share/virtualenvs/api-RqAznZCl/lib/python3.7/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "/Users/.local/share/virtualenvs/api-RqAznZCl/lib/python3.7/site-packages/django/views/generic/base.py", line 69, in view
    return self.dispatch(request, *args, **kwargs)
  File "/Users/.local/share/virtualenvs/api-RqAznZCl/lib/python3.7/site-packages/rest_framework/views.py", line 483, in dispatch
    response = self.handle_exception(exc)
  File "/Users/.local/share/virtualenvs/api-RqAznZCl/lib/python3.7/site-packages/rest_framework/views.py", line 443, in handle_exception
    self.raise_uncaught_exception(exc)
  File "/Users/.local/share/virtualenvs/api-RqAznZCl/lib/python3.7/site-packages/rest_framework/views.py", line 480, in dispatch
    response = handler(request, *args, **kwargs)
  File "/Users/repos/api/core/views.py", line 30, in post
    if serializer.is_valid():
  File "/Users/.local/share/virtualenvs/api-RqAznZCl/lib/python3.7/site-packages/rest_framework/serializers.py", line 236, in is_valid
    self._validated_data = self.run_validation(self.initial_data)
  File "/Users/.local/share/virtualenvs/api-RqAznZCl/lib/python3.7/site-packages/rest_framework/serializers.py", line 434, in run_validation
    value = self.to_internal_value(data)
  File "/Users/.local/share/virtualenvs/api-RqAznZCl/lib/python3.7/site-packages/rest_framework/serializers.py", line 482, in to_internal_value
    fields = self._writable_fields
  File "/Users/.local/share/virtualenvs/api-RqAznZCl/lib/python3.7/site-packages/django/utils/functional.py", line 36, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/Users/.local/share/virtualenvs/api-RqAznZCl/lib/python3.7/site-packages/rest_framework/serializers.py", line 370, in _writable_fields
    field for field in self.fields.values() if not field.read_only
  File "/Users/.local/share/virtualenvs/api-RqAznZCl/lib/python3.7/site-packages/rest_framework/serializers.py", line 363, in fields
    for key, value in self.get_fields().items():
  File "/Users/.local/share/virtualenvs/api-RqAznZCl/lib/python3.7/site-packages/rest_framework/serializers.py", line 1019, in get_fields
    info = model_meta.get_field_info(model)
  File "/Users/.local/share/virtualenvs/api-RqAznZCl/lib/python3.7/site-packages/rest_framework/utils/model_meta.py", line 39, in get_field_info
    forward_relations = _get_forward_relationships(opts)
  File "/Users/.local/share/virtualenvs/api-RqAznZCl/lib/python3.7/site-packages/rest_framework/utils/model_meta.py", line 96, in _get_forward_relationships
    not field.remote_field.through._meta.auto_created
AttributeError: 'NoneType' object has no attribute '_meta'

好像ti与序列化程序有关,因为我使用pdb进行跟踪并且正在生成令牌,但是传递回的用户对象未正确序列化...

1 个答案:

答案 0 :(得分:0)

似乎djangorestframework-jwt库从URL logoIconURL = getClass().getResource(""); //path of your image file ImageIcon logoIcon = new ImageIcon(logoIconURL); frame.setIconImage(logoIcon.getImage()); 中提取了User模型,因此请确保get_user_model指向自定义用户模型类。