1
這是一個錯誤還是我錯了? 我在步驟來創建一個超級用戶,但Django的希望表錯分貝儘管我的路由器似乎工作:Django無法在其他分區創建超級用戶而不是'默認'
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'替代) 但是,我的路由器被調用並返回正確的結果,因爲我們在命令輸出中看到「READ auth」和「TRUE」...
任何想法?
任何你不使用django.db.backends.mysql作爲數據庫引擎的理由? – rnevius 2015-02-11 12:45:20
Python 3,還沒有發佈;) – 2015-02-11 17:32:53
在運行'createsuperuser'之前,你不需要'syncdb'嗎? – favoretti 2015-02-11 17:50:40