在Django中迁移并创建超级用户后,“没有这样的表:accounts_user”

时间:2019-11-23 17:41:42

标签: python django django-models django-rest-framework django-rest-auth

我正在使用 Django-rest-auth 创建rest API,并且在名为accounts的应用程序中有一个自定义用户模型。问题是进行迁移后,当我尝试在控制台中创建超级用户后,在电子邮件字段中输入电子邮件后,

我遇到很多错误,告诉我"no such table: accounts_user"

我的设置。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",
}

models.py

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)

我尝试删除db.sqlite3并重新运行迁移,但仍然是相同的错误。

我也尝试运行"python manage.py migrate --run-syncdb",该方法可以正常工作,但是后来发现,当我尝试重置密码时,出现了一个错误,提示没有这样的表"accounts_user.fullname"

运行python manage.py showmigrations,我得到:

account
 [X] 0001_initial
 [X] 0002_email_max_length
admin
 [X] 0001_initial
 [X] 0002_logentry_remove_auto_add
 [X] 0003_logentry_add_action_flag_choices
auth
 [X] 0001_initial
 [X] 0002_alter_permission_name_max_length
 [X] 0003_alter_user_email_max_length
 [X] 0004_alter_user_username_opts
 [X] 0005_alter_user_last_login_null
 [X] 0006_require_contenttypes_0002
 [X] 0007_alter_validators_add_error_messages
 [X] 0008_alter_user_username_max_length
 [X] 0009_alter_user_last_name_max_length
 [X] 0010_alter_group_name_max_length
 [X] 0011_update_proxy_permissions
authtoken
 [X] 0001_initial
 [X] 0002_auto_20160226_1747
contenttypes
 [X] 0001_initial
 [X] 0002_remove_content_type_name
sessions
 [X] 0001_initial
sites
 [X] 0001_initial
 [X] 0002_alter_domain_unique
socialaccount
 [X] 0001_initial
 [X] 0002_token_max_lengths
 [X] 0003_extra_data_default_dict

1 个答案:

答案 0 :(得分:0)

请尝试再次使用makemigrations。看起来您已经对模型进行了一些更改,但是这些更改未反映在迁移文件中。 https://docs.djangoproject.com/en/2.2/topics/migrations/#workflow