Django 无法在 'default' 之外的其他数据库中创建超级用户
Django fails to create superuser in other db than 'default'
这是错误还是我错了?
我正在创建一个超级用户,但 django 想要 table 在错误的数据库中,尽管我的路由器似乎可以工作:
settings.py
DATABASES = {
'intern_db': {
'ENGINE': 'mysql.connector.django',
'NAME': 'django_cartons',
'USER': 'root',
'PASSWORD' : '',
},
'default': {
'ENGINE': 'mysql.connector.django',
'NAME': 'cartons',
'USER': 'root',
'PASSWORD' : '',
}
}
DATABASE_ROUTERS = ['web.routers.AuthRouter']
routers.py
class AuthRouter(object):
"""
A router to control all database operations on models in the
auth application.
"""
def db_for_read(self, model, **hints):
"""
Attempts to read auth models go to auth.
"""
print("READ ",model._meta.app_label)
if model._meta.app_label in ['auth', 'contenttypes', 'admin', 'sessions']:
print(True)
return 'intern_db'
return None
def db_for_write(self, model, **hints):
"""
Attempts to write auth models go to auth.
"""
print("WRITE ",model._meta.app_label)
if model._meta.app_label in ['auth', 'contenttypes', 'admin', 'sessions']:
print(True)
return 'intern_db'
return None
def allow_relation(self, obj1, obj2, **hints):
"""
Allow relations if a model in the auth app is involved.
"""
print("REL ", obj1._meta.app_label, ' ', obj2._meta.app_label)
if obj1._meta.app_label in ['auth', 'contenttypes', 'admin', 'sessions'] or \
obj2._meta.app_label in ['auth', 'contenttypes', 'admin', 'sessions']:
return True
return None
def allow_migrate(self, db, model):
"""
Make sure the auth app only appears in the 'auth'
database.
"""
if db == 'intern_db':
return (model._meta.app_label in ['auth', 'contenttypes', 'admin', 'sessions'])
elif model._meta.app_label in ['auth', 'contenttypes', 'admin', 'sessions']:
return False
return None
命令:
$> ./manage.py createsuperuser
READ auth
True
READ auth
True
Username (leave blank to use 'leo'): admin
Traceback (most recent call last):
File "/usr/lib/python3.4/site-packages/mysql/connector/django/base.py", line 115, in _execute_wrapper
return method(query, args)
File "/usr/lib/python3.4/site-packages/mysql/connector/cursor.py", line 507, in execute
self._handle_result(self._connection.cmd_query(stmt))
File "/usr/lib/python3.4/site-packages/mysql/connector/connection.py", line 722, in cmd_query
result = self._handle_result(self._send_cmd(ServerCmd.QUERY, query))
File "/usr/lib/python3.4/site-packages/mysql/connector/connection.py", line 640, in _handle_result
raise errors.get_exception(packet)
mysql.connector.errors.ProgrammingError: 1146 (42S02): Table 'cartons.auth_user' doesn't exist
如您所见,它寻找不存在的 'cartons.auth_user'(它应该是 'django_cartons',别名为 'intern_db')
然而,我的路由器被调用并且 return 正确的结果正如我们在命令输出中看到的 "READ auth" 和 "TRUE"...
有什么想法吗?
问题是系统有些损坏:它尊重某些任务的配置,但不尊重其他任务(输出中第 2 个 "TRUE"),但不尊重其他任务并使用默认值。
这可能是有意为之,但很奇怪(实际上没有什么禁止拥有多个管理数据库,并且允许没有自动暗选择)。
实际上要在另一个数据库中创建 SU,并且对于这些命令的任何用法,您必须传递要创建它的数据库显式 :
./manage.py createsuperuser --database=intern_db
注意:数据库名称是配置中的别名。
您需要在创建超级用户之前迁移到您的数据库。进行迁移
user@root:~$python manage.py migrate
这将在您的数据库中创建 auth_user
table。
这是错误还是我错了? 我正在创建一个超级用户,但 django 想要 table 在错误的数据库中,尽管我的路由器似乎可以工作:
settings.py
DATABASES = {
'intern_db': {
'ENGINE': 'mysql.connector.django',
'NAME': 'django_cartons',
'USER': 'root',
'PASSWORD' : '',
},
'default': {
'ENGINE': 'mysql.connector.django',
'NAME': 'cartons',
'USER': 'root',
'PASSWORD' : '',
}
}
DATABASE_ROUTERS = ['web.routers.AuthRouter']
routers.py
class AuthRouter(object):
"""
A router to control all database operations on models in the
auth application.
"""
def db_for_read(self, model, **hints):
"""
Attempts to read auth models go to auth.
"""
print("READ ",model._meta.app_label)
if model._meta.app_label in ['auth', 'contenttypes', 'admin', 'sessions']:
print(True)
return 'intern_db'
return None
def db_for_write(self, model, **hints):
"""
Attempts to write auth models go to auth.
"""
print("WRITE ",model._meta.app_label)
if model._meta.app_label in ['auth', 'contenttypes', 'admin', 'sessions']:
print(True)
return 'intern_db'
return None
def allow_relation(self, obj1, obj2, **hints):
"""
Allow relations if a model in the auth app is involved.
"""
print("REL ", obj1._meta.app_label, ' ', obj2._meta.app_label)
if obj1._meta.app_label in ['auth', 'contenttypes', 'admin', 'sessions'] or \
obj2._meta.app_label in ['auth', 'contenttypes', 'admin', 'sessions']:
return True
return None
def allow_migrate(self, db, model):
"""
Make sure the auth app only appears in the 'auth'
database.
"""
if db == 'intern_db':
return (model._meta.app_label in ['auth', 'contenttypes', 'admin', 'sessions'])
elif model._meta.app_label in ['auth', 'contenttypes', 'admin', 'sessions']:
return False
return None
命令:
$> ./manage.py createsuperuser
READ auth
True
READ auth
True
Username (leave blank to use 'leo'): admin
Traceback (most recent call last):
File "/usr/lib/python3.4/site-packages/mysql/connector/django/base.py", line 115, in _execute_wrapper
return method(query, args)
File "/usr/lib/python3.4/site-packages/mysql/connector/cursor.py", line 507, in execute
self._handle_result(self._connection.cmd_query(stmt))
File "/usr/lib/python3.4/site-packages/mysql/connector/connection.py", line 722, in cmd_query
result = self._handle_result(self._send_cmd(ServerCmd.QUERY, query))
File "/usr/lib/python3.4/site-packages/mysql/connector/connection.py", line 640, in _handle_result
raise errors.get_exception(packet)
mysql.connector.errors.ProgrammingError: 1146 (42S02): Table 'cartons.auth_user' doesn't exist
如您所见,它寻找不存在的 'cartons.auth_user'(它应该是 'django_cartons',别名为 'intern_db') 然而,我的路由器被调用并且 return 正确的结果正如我们在命令输出中看到的 "READ auth" 和 "TRUE"...
有什么想法吗?
问题是系统有些损坏:它尊重某些任务的配置,但不尊重其他任务(输出中第 2 个 "TRUE"),但不尊重其他任务并使用默认值。
这可能是有意为之,但很奇怪(实际上没有什么禁止拥有多个管理数据库,并且允许没有自动暗选择)。
实际上要在另一个数据库中创建 SU,并且对于这些命令的任何用法,您必须传递要创建它的数据库显式 :
./manage.py createsuperuser --database=intern_db
注意:数据库名称是配置中的别名。
您需要在创建超级用户之前迁移到您的数据库。进行迁移
user@root:~$python manage.py migrate
这将在您的数据库中创建 auth_user
table。