Python “;没有这样的表:accounts#u user”;在Django中迁移并创建超级用户之后
我正在使用Django rest auth创建一个rest API,我在一个名为accounts的应用程序中有一个自定义用户模型。问题是在迁移之后,当我在“电子邮件”字段中输入电子邮件后尝试在控制台中创建超级用户时 我收到一大堆错误,告诉我“没有这样的表:帐户\用户” 我的设置.pyPython “;没有这样的表:accounts#u user”;在Django中迁移并创建超级用户之后,python,django,django-models,django-rest-framework,django-rest-auth,Python,Django,Django Models,Django Rest Framework,Django Rest Auth,我正在使用Django rest auth创建一个rest API,我在一个名为accounts的应用程序中有一个自定义用户模型。问题是在迁移之后,当我在“电子邮件”字段中输入电子邮件后尝试在控制台中创建超级用户时 我收到一大堆错误,告诉我“没有这样的表:帐户\用户” 我的设置.py INSTALLED_APPS = [ ... 'django.contrib.sites', 'rest_framework', 'rest_framework.authtoken'
INSTALLED_APPS = [
...
'django.contrib.sites',
'rest_framework',
'rest_framework.authtoken',
'rest_auth',
'rest_auth.registration',
'allauth',
'allauth.account',
'allauth.socialaccount',
'allauth.socialaccount.providers.google',
'allauth.socialaccount.providers.facebook',
'accounts',
]
AUTH_USER_MODEL = 'accounts.User'
# to use old_password when setting a new password
OLD_PASSWORD_FIELD_ENABLED = True
LOGOUT_ON_PASSWORD_CHANGE = False
ACCOUNT_USER_MODEL_USERNAME_FIELD = None
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_USERNAME_REQUIRED = False
ACCOUNT_AUTHENTICATION_METHOD = 'email'
ACCOUNT_USER_EMAIL_FIELD = 'email'
ACCOUNT_UNIQUE_EMAIL = True
ACCOUNT_LOGOUT_ON_GET = True
# UNSURE
ACCOUNT_EMAIL_CONFIRMATION_EXPIRE_DAYS = 1
ACCOUNT_EMAIL_VERIFICATION = "mandatory"
ACCOUNT_LOGIN_ATTEMPTS_LIMIT = 5
ACCOUNT_LOGIN_ATTEMPTS_TIMEOUT = 86400 # 1 day in seconds
ACCOUNT_LOGOUT_REDIRECT_URL ='api/accounts/rest-auth/login/'
LOGIN_REDIRECT_URL = 'api/accounts/rest-auth/user/'
SOCIALACCOUNT_EMAIL_VERIFICATION = 'none'
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'opeyemiodedeyi@gmail.com'
EMAIL_HOST_PASSWORD = '9j@4lifE'
DEFAULT_FROM_EMAIL = 'opeyemiodedeyi@gmail.com'
DEFAULT_TO_EMAIL = EMAIL_HOST_USER
EMAIL_CONFIRMATION_AUTHENTICATED_REDIRECT_URL = '/'
REST_AUTH_SERIALIZERS = {
"USER_DETAILS_SERIALIZER": "accounts.api.serializers.CustomUserDetailsSerializer",
}
REST_AUTH_REGISTER_SERIALIZERS = {
"REGISTER_SERIALIZER": "accounts.api.serializers.CustomRegisterSerializer",
}
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager, PermissionsMixin
from django.db import models
from django.utils import timezone
class UserManager(BaseUserManager):
def _create_user(self, email, fullname, password, is_staff, is_superuser, **extra_fields):
if not email:
raise ValueError('Users must have an email address')
now = timezone.now()
email = self.normalize_email(email)
user = self.model(
email=email,
fullname=fullname,
is_staff=is_staff,
is_active=True,
is_superuser=is_superuser,
last_login=now,
date_joined=now,
**extra_fields
)
user.set_password(password)
user.save(using=self._db)
return user
def create_user(self, email, fullname, password, **extra_fields):
return self._create_user(email, fullname, password, False, False, **extra_fields)
def create_superuser(self, email, fullname, password, **extra_fields):
user=self._create_user(email, fullname, password, True, True, **extra_fields)
user.save(using=self._db)
return user
class User(AbstractBaseUser, PermissionsMixin):
username = None
email = models.EmailField(max_length=254, unique=True)
fullname = models.CharField(max_length=250)
is_staff = models.BooleanField(default=False)
is_superuser = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
last_login = models.DateTimeField(null=True, blank=True)
date_joined = models.DateTimeField(auto_now_add=True)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
objects = UserManager()
def __str__(self):
return self.email
型号.py
INSTALLED_APPS = [
...
'django.contrib.sites',
'rest_framework',
'rest_framework.authtoken',
'rest_auth',
'rest_auth.registration',
'allauth',
'allauth.account',
'allauth.socialaccount',
'allauth.socialaccount.providers.google',
'allauth.socialaccount.providers.facebook',
'accounts',
]
AUTH_USER_MODEL = 'accounts.User'
# to use old_password when setting a new password
OLD_PASSWORD_FIELD_ENABLED = True
LOGOUT_ON_PASSWORD_CHANGE = False
ACCOUNT_USER_MODEL_USERNAME_FIELD = None
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_USERNAME_REQUIRED = False
ACCOUNT_AUTHENTICATION_METHOD = 'email'
ACCOUNT_USER_EMAIL_FIELD = 'email'
ACCOUNT_UNIQUE_EMAIL = True
ACCOUNT_LOGOUT_ON_GET = True
# UNSURE
ACCOUNT_EMAIL_CONFIRMATION_EXPIRE_DAYS = 1
ACCOUNT_EMAIL_VERIFICATION = "mandatory"
ACCOUNT_LOGIN_ATTEMPTS_LIMIT = 5
ACCOUNT_LOGIN_ATTEMPTS_TIMEOUT = 86400 # 1 day in seconds
ACCOUNT_LOGOUT_REDIRECT_URL ='api/accounts/rest-auth/login/'
LOGIN_REDIRECT_URL = 'api/accounts/rest-auth/user/'
SOCIALACCOUNT_EMAIL_VERIFICATION = 'none'
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'opeyemiodedeyi@gmail.com'
EMAIL_HOST_PASSWORD = '9j@4lifE'
DEFAULT_FROM_EMAIL = 'opeyemiodedeyi@gmail.com'
DEFAULT_TO_EMAIL = EMAIL_HOST_USER
EMAIL_CONFIRMATION_AUTHENTICATED_REDIRECT_URL = '/'
REST_AUTH_SERIALIZERS = {
"USER_DETAILS_SERIALIZER": "accounts.api.serializers.CustomUserDetailsSerializer",
}
REST_AUTH_REGISTER_SERIALIZERS = {
"REGISTER_SERIALIZER": "accounts.api.serializers.CustomRegisterSerializer",
}
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager, PermissionsMixin
from django.db import models
from django.utils import timezone
class UserManager(BaseUserManager):
def _create_user(self, email, fullname, password, is_staff, is_superuser, **extra_fields):
if not email:
raise ValueError('Users must have an email address')
now = timezone.now()
email = self.normalize_email(email)
user = self.model(
email=email,
fullname=fullname,
is_staff=is_staff,
is_active=True,
is_superuser=is_superuser,
last_login=now,
date_joined=now,
**extra_fields
)
user.set_password(password)
user.save(using=self._db)
return user
def create_user(self, email, fullname, password, **extra_fields):
return self._create_user(email, fullname, password, False, False, **extra_fields)
def create_superuser(self, email, fullname, password, **extra_fields):
user=self._create_user(email, fullname, password, True, True, **extra_fields)
user.save(using=self._db)
return user
class User(AbstractBaseUser, PermissionsMixin):
username = None
email = models.EmailField(max_length=254, unique=True)
fullname = models.CharField(max_length=250)
is_staff = models.BooleanField(default=False)
is_superuser = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
last_login = models.DateTimeField(null=True, blank=True)
date_joined = models.DateTimeField(auto_now_add=True)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
objects = UserManager()
def __str__(self):
return self.email
我收到的错误:
Traceback (most recent call last):
File "D:\Opeodedeyi\Documents\Django\WORK IN PROGRESS\creative-app-api\venv\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "D:\Opeodedeyi\Documents\Django\WORK IN PROGRESS\creative-app-api\venv\lib\site-packages\django\db\backends\sqlite3\base.py", line 383, in execute
return Database.Cursor.execute(self, query, params)
sqlite3.OperationalError: no such table: accounts_user
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 21, in <module>
main()
File "manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "D:\Opeodedeyi\Documents\Django\WORK IN PROGRESS\creative-app-api\venv\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
utility.execute()
File "D:\Opeodedeyi\Documents\Django\WORK IN PROGRESS\creative-app-api\venv\lib\site-packages\django\core\management\__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "D:\Opeodedeyi\Documents\Django\WORK IN PROGRESS\creative-app-api\venv\lib\site-packages\django\core\management\base.py", line 323, in run_from_argv
self.execute(*args, **cmd_options)
File "D:\Opeodedeyi\Documents\Django\WORK IN PROGRESS\creative-app-api\venv\lib\site-packages\django\contrib\auth\management\commands\createsuperuser.py", line 61, in execute
return super().execute(*args, **options)
File "D:\Opeodedeyi\Documents\Django\WORK IN PROGRESS\creative-app-api\venv\lib\site-packages\django\core\management\base.py", line 364, in execute
output = self.handle(*args, **options)
File "D:\Opeodedeyi\Documents\Django\WORK IN PROGRESS\creative-app-api\venv\lib\site-packages\django\contrib\auth\management\commands\createsuperuser.py", line 95, in handle
error_msg = self._validate_username(username, verbose_field_name,
database)
File "D:\Opeodedeyi\Documents\Django\WORK IN PROGRESS\creative-app-api\venv\lib\site-packages\django\contrib\auth\management\commands\createsuperuser.py", line 201, in _validate_username
self.UserModel._default_manager.db_manager(database).get_by_natural_key(username)
File "D:\Opeodedeyi\Documents\Django\WORK IN PROGRESS\creative-app-api\venv\lib\site-packages\django\contrib\auth\base_user.py", line 44,
in get_by_natural_key
return self.get(**{self.model.USERNAME_FIELD: username})
File "D:\Opeodedeyi\Documents\Django\WORK IN PROGRESS\creative-app-api\venv\lib\site-packages\django\db\models\manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "D:\Opeodedeyi\Documents\Django\WORK IN PROGRESS\creative-app-api\venv\lib\site-packages\django\db\models\query.py", line 402, in get
num = len(clone)
File "D:\Opeodedeyi\Documents\Django\WORK IN PROGRESS\creative-app-api\venv\lib\site-packages\django\db\models\query.py", line 256, in __len__
self._fetch_all()
File "D:\Opeodedeyi\Documents\Django\WORK IN PROGRESS\creative-app-api\venv\lib\site-packages\django\db\models\query.py", line 1242, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "D:\Opeodedeyi\Documents\Django\WORK IN PROGRESS\creative-app-api\venv\lib\site-packages\django\db\models\query.py", line 55, in __iter__
results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
File "D:\Opeodedeyi\Documents\Django\WORK IN PROGRESS\creative-app-api\venv\lib\site-packages\django\db\models\sql\compiler.py", line 1100, in execute_sql
cursor.execute(sql, params)
File "D:\Opeodedeyi\Documents\Django\WORK IN PROGRESS\creative-app-api\venv\lib\site-packages\django\db\backends\utils.py", line 99, in execute
return super().execute(sql, params)
File "D:\Opeodedeyi\Documents\Django\WORK IN PROGRESS\creative-app-api\venv\lib\site-packages\django\db\backends\utils.py", line 67, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "D:\Opeodedeyi\Documents\Django\WORK IN PROGRESS\creative-app-api\venv\lib\site-packages\django\db\backends\utils.py", line 76, in _execute_with_wrappers
return executor(sql, params, many, context)
File "D:\Opeodedeyi\Documents\Django\WORK IN PROGRESS\creative-app-api\venv\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "D:\Opeodedeyi\Documents\Django\WORK IN PROGRESS\creative-app-api\venv\lib\site-packages\django\db\utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "D:\Opeodedeyi\Documents\Django\WORK IN PROGRESS\creative-app-api\venv\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "D:\Opeodedeyi\Documents\Django\WORK IN PROGRESS\creative-app-api\venv\lib\site-packages\django\db\backends\sqlite3\base.py", line 383, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such table: accounts_user
(venv)
请再次尝试使用
makemigrations
。看起来您在模型中做了一些更改,但这些更改没有反映在迁移文件中。
为什么不在settings.py中设置一个
AUTH\u USER\u MODEL
设置呢?我确实有,我已经更新了代码