TypeError: Abstract base class containing model fields not permitted for proxy model 'TokenProxy'. when trying to makemigrations in Django
TypeError: Abstract base class containing model fields not permitted for proxy model 'TokenProxy'. when trying to makemigrations in Django
我创建了一个自定义用户模型来用电子邮件替换用户名,并希望将其与 Djoser 和 React 一起使用。
但是当我尝试进行迁移时出现此错误(更多详细信息如下):
TypeError: Abstract base class containing model fields not permitted for proxy model 'TokenProxy'.
models.py:
from django.db import models
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager, PermissionsMixin
class UserAccountManager(BaseUserManager):
def create_user(self, email, name, password=None):
if not email:
raise ValueError('Users must have an email address')
email = self.normalize_email(email)
user = self.model(email=email, name=name)
user.set_password(password)
user.save()
return user
def create_superuser(self, email, password, name, **extra_fields):
"""Create and save a SuperUser with the given email and password."""
extra_fields.setdefault('is_staff', True)
extra_fields.setdefault('is_superuser', True)
if extra_fields.get('is_staff') is not True:
raise ValueError('Superuser must have is_staff=True.')
if extra_fields.get('is_superuser') is not True:
raise ValueError('Superuser must have is_superuser=True.')
serializers.py
from djoser.serializers import UserCreateSerializer, UserSerializer
from django.contrib.auth import get_user_model
User = get_user_model()
class UserCreateSerializer(UserCreateSerializer):
class Meta(UserCreateSerializer.Meta):
model = User
fields = ('id', 'email', 'name', 'password')
urls.py
urlpatterns = [
path('auth/', include('djoser.urls')),
path('auth/', include('djoser.urls.jwt')),
]
urlpatterns += [re_path(r'^.*', TemplateView.as_view(template_name='index.html'))]
我在尝试进行迁移时遇到此错误:
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "C:\Users\Jonas\djangoProjects\AntDesign\coursesApi\urls.py", line 10, in <module>
path('api/', include('api.urls')),
File "C:\Users\Jonas\.virtualenvs\AntDesign-TgIU7Y6b\lib\site-packages\django\urls\conf.py", line 34, in include
urlconf_module = import_module(urlconf_module)
File "c:\users\jonas\appdata\local\programs\python\python37\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "C:\Users\Jonas\djangoProjects\AntDesign\api\urls.py", line 3, in <module>
from .views import CoursesListView, CoursesDetailView, VideoListView, VideoDetailView
File "C:\Users\Jonas\djangoProjects\AntDesign\api\views.py", line 4, in <module>
from .serializers import CourseSerializer, VideoSerializer
File "C:\Users\Jonas\djangoProjects\AntDesign\api\serializers.py", line 1, in <module>
from djoser.serializers import UserCreateSerializer, UserSerializer
File "C:\Users\Jonas\.virtualenvs\AntDesign-TgIU7Y6b\lib\site-packages\djoser\serializers.py", line 15, in <module>
class UserSerializer(serializers.ModelSerializer):
File "C:\Users\Jonas\.virtualenvs\AntDesign-TgIU7Y6b\lib\site-packages\djoser\serializers.py", line 16, in UserSerializer
class Meta:
File "C:\Users\Jonas\.virtualenvs\AntDesign-TgIU7Y6b\lib\site-packages\djoser\serializers.py", line 19, in Meta
settings.USER_ID_FIELD,
File "C:\Users\Jonas\.virtualenvs\AntDesign-TgIU7Y6b\lib\site-packages\django\utils\functional.py", line 256, in inner
self._setup()
File "C:\Users\Jonas\.virtualenvs\AntDesign-TgIU7Y6b\lib\site-packages\djoser\conf.py", line 140, in _setup
self._wrapped = Settings(default_settings, explicit_overriden_settings)
File "C:\Users\Jonas\.virtualenvs\AntDesign-TgIU7Y6b\lib\site-packages\djoser\conf.py", line 116, in __init__
self._init_settings_to_import()
File "C:\Users\Jonas\.virtualenvs\AntDesign-TgIU7Y6b\lib\site-packages\djoser\conf.py", line 135, in _init_settings_to_import
setattr(self, setting_name, import_string(value))
File "C:\Users\Jonas\.virtualenvs\AntDesign-TgIU7Y6b\lib\site-packages\django\utils\module_loading.py", line 17, in import_string
module = import_module(module_path)
File "c:\users\jonas\appdata\local\programs\python\python37\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "C:\Users\Jonas\.virtualenvs\AntDesign-TgIU7Y6b\lib\site-packages\rest_framework\authtoken\models.py", line 43, in <module>
class TokenProxy(Token):
File "C:\Users\Jonas\.virtualenvs\AntDesign-TgIU7Y6b\lib\site-packages\django\db\models\base.py", line 174, in __new__
"permitted for proxy model '%s'." % name
TypeError: Abstract base class containing model fields not permitted for proxy model 'TokenProxy'.
请检查django-rest-framework
中的following PR。
作为解决方法,您可以在 djoser
之前添加 rest_framework.authtoken
到 INSTALLED_APPS
。
我创建了一个自定义用户模型来用电子邮件替换用户名,并希望将其与 Djoser 和 React 一起使用。
但是当我尝试进行迁移时出现此错误(更多详细信息如下):
TypeError: Abstract base class containing model fields not permitted for proxy model 'TokenProxy'.
models.py:
from django.db import models
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager, PermissionsMixin
class UserAccountManager(BaseUserManager):
def create_user(self, email, name, password=None):
if not email:
raise ValueError('Users must have an email address')
email = self.normalize_email(email)
user = self.model(email=email, name=name)
user.set_password(password)
user.save()
return user
def create_superuser(self, email, password, name, **extra_fields):
"""Create and save a SuperUser with the given email and password."""
extra_fields.setdefault('is_staff', True)
extra_fields.setdefault('is_superuser', True)
if extra_fields.get('is_staff') is not True:
raise ValueError('Superuser must have is_staff=True.')
if extra_fields.get('is_superuser') is not True:
raise ValueError('Superuser must have is_superuser=True.')
serializers.py
from djoser.serializers import UserCreateSerializer, UserSerializer
from django.contrib.auth import get_user_model
User = get_user_model()
class UserCreateSerializer(UserCreateSerializer):
class Meta(UserCreateSerializer.Meta):
model = User
fields = ('id', 'email', 'name', 'password')
urls.py
urlpatterns = [
path('auth/', include('djoser.urls')),
path('auth/', include('djoser.urls.jwt')),
]
urlpatterns += [re_path(r'^.*', TemplateView.as_view(template_name='index.html'))]
我在尝试进行迁移时遇到此错误:
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "C:\Users\Jonas\djangoProjects\AntDesign\coursesApi\urls.py", line 10, in <module>
path('api/', include('api.urls')),
File "C:\Users\Jonas\.virtualenvs\AntDesign-TgIU7Y6b\lib\site-packages\django\urls\conf.py", line 34, in include
urlconf_module = import_module(urlconf_module)
File "c:\users\jonas\appdata\local\programs\python\python37\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "C:\Users\Jonas\djangoProjects\AntDesign\api\urls.py", line 3, in <module>
from .views import CoursesListView, CoursesDetailView, VideoListView, VideoDetailView
File "C:\Users\Jonas\djangoProjects\AntDesign\api\views.py", line 4, in <module>
from .serializers import CourseSerializer, VideoSerializer
File "C:\Users\Jonas\djangoProjects\AntDesign\api\serializers.py", line 1, in <module>
from djoser.serializers import UserCreateSerializer, UserSerializer
File "C:\Users\Jonas\.virtualenvs\AntDesign-TgIU7Y6b\lib\site-packages\djoser\serializers.py", line 15, in <module>
class UserSerializer(serializers.ModelSerializer):
File "C:\Users\Jonas\.virtualenvs\AntDesign-TgIU7Y6b\lib\site-packages\djoser\serializers.py", line 16, in UserSerializer
class Meta:
File "C:\Users\Jonas\.virtualenvs\AntDesign-TgIU7Y6b\lib\site-packages\djoser\serializers.py", line 19, in Meta
settings.USER_ID_FIELD,
File "C:\Users\Jonas\.virtualenvs\AntDesign-TgIU7Y6b\lib\site-packages\django\utils\functional.py", line 256, in inner
self._setup()
File "C:\Users\Jonas\.virtualenvs\AntDesign-TgIU7Y6b\lib\site-packages\djoser\conf.py", line 140, in _setup
self._wrapped = Settings(default_settings, explicit_overriden_settings)
File "C:\Users\Jonas\.virtualenvs\AntDesign-TgIU7Y6b\lib\site-packages\djoser\conf.py", line 116, in __init__
self._init_settings_to_import()
File "C:\Users\Jonas\.virtualenvs\AntDesign-TgIU7Y6b\lib\site-packages\djoser\conf.py", line 135, in _init_settings_to_import
setattr(self, setting_name, import_string(value))
File "C:\Users\Jonas\.virtualenvs\AntDesign-TgIU7Y6b\lib\site-packages\django\utils\module_loading.py", line 17, in import_string
module = import_module(module_path)
File "c:\users\jonas\appdata\local\programs\python\python37\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "C:\Users\Jonas\.virtualenvs\AntDesign-TgIU7Y6b\lib\site-packages\rest_framework\authtoken\models.py", line 43, in <module>
class TokenProxy(Token):
File "C:\Users\Jonas\.virtualenvs\AntDesign-TgIU7Y6b\lib\site-packages\django\db\models\base.py", line 174, in __new__
"permitted for proxy model '%s'." % name
TypeError: Abstract base class containing model fields not permitted for proxy model 'TokenProxy'.
请检查django-rest-framework
中的following PR。
作为解决方法,您可以在 djoser
之前添加 rest_framework.authtoken
到 INSTALLED_APPS
。