gpt4 book ai didi

python - Django 无法在 'default' 之外的其他数据库中创建 super 用户

转载 作者:太空宇宙 更新时间:2023-11-04 01:09:35 24 4
gpt4 key购买 nike

这是错误还是我错了?我正在创建一个 super 用户,但是尽管我的路由器似乎可以工作,但 django 想要一个错误数据库中的表:

设置.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']

路由器.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”)然而,我的路由器被调用并返回正确的结果,因为我们在命令输出中看到“READ auth”和“TRUE”...

有什么想法吗?

最佳答案

问题是系统有些损坏:它尊重某些任务的配置但不尊重其他任务(输出中的第 2 个“TRUE”)但不尊重其他任务并使用默认设置。

这可能是有意为之,但很奇怪(实际上没有什么禁止拥有多个管理数据库,并且允许没有自动暗选择)。

实际上,要在另一个数据库中创建 SU,并且对于这些命令的任何用法,您必须将要创建它的数据库传递给显式:

./manage.py createsuperuser --database=intern_db

注意:数据库名称是配置中的别名。

关于python - Django 无法在 'default' 之外的其他数据库中创建 super 用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28453810/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com