Django:尝试从管理员创建用户时出现OperationalError [英] Django: OperationalError when I try to create user from admin
问题描述
我使用MySQL的Django 1.10,每次输入admin并尝试使用户时都会出现此错误:
I have Django 1.10 with MySQL, every time I enter admin and I try to make a user I get this error:
问题是我可以创建任何其他模型,如果我从命令行创建超级用户,则可以编辑该用户,但是当我按添加用户时,我得到这个错误。我尝试删除数据库并再次创建两次。
The thing is I can create any other model and if I create a super user from command line I can edit that user but when I press "add user" I get this error. I tried deleting the database and making it again twice.
这是我的settings.py:
And here is my settings.py:
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
DEBUG = True
ALLOWED_HOSTS = []
INSTALLED_APPS = [
# django apps
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
# our apps
'authentication',
'homepages',
'school'
]
MIDDLEWARE_CLASSES = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'config.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
BASE_DIR + '/templates/'
],
'APP_DIRS': True,
'OPTIONS': {
'debug': DEBUG,
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'config.wsgi.application'
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'isj',
'USER': 'test_user',
'PASSWORD': 'password',
'HOST': '', # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
'PORT': '', # Set to empty string for default.
}
}
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
STATIC_URL = '/static/'
STATICFILES_DIRS = ( os.path.join('static'), )
推荐答案
对于我来说,我发现根本原因是 MySQL-Python
数据库驱动程序,解决方法是将其他MySQL驱动程序用于Python(例如 mysqlclient
)。
In my case, I've found out that the root cause is the MySQL-Python
database driver and the fix is to use other MySQL drivers for Python (e.g. mysqlclient
).
$ pip uninstall MySQL-Python
$ pip install mysqlclient
MySQL-python
原来很旧(最后一次提交是在7年前)。 mysqlclient
是它的现代版本,具有许多改进和错误修复。
MySQL-python
turned out to be very old (last commit was 7 years ago). mysqlclient
is the modern version of it, with lots of improvements and bug fixes.
有关我的修复和更多详细信息调查结果此处。
More details about my fix and findings here.
这篇关于Django:尝试从管理员创建用户时出现OperationalError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!