我已经开始了一个新的 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 进行跟踪并且正在生成令牌,但传回的用户对象没有被正确序列化......