Python Django无法在除';默认值';

Python Django无法在除';默认值';,python,django,multi-database,Python,Django,Multi Database,是虫子还是我错了? 我正在创建超级用户,但django希望在错误的数据库中创建一个表,尽管我的路由器似乎工作正常: 设置.py DATABASES = { 'intern_db': { 'ENGINE': 'mysql.connector.django', 'NAME': 'django_cartons', 'USER': 'root', 'PASSWORD' : '', }, 'de

是虫子还是我错了? 我正在创建超级用户,但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”) 然而,当我们在命令输出中看到“readauth”和“TRUE”时,我的路由器被调用并返回正确的结果


有什么想法吗?

问题是系统有点崩溃:它尊重某些任务的配置,但不尊重其他任务的配置(输出中的第一个“TRUE”为2),但不尊重其他任务的配置,并使用默认值

这可能是故意的,即使很奇怪(实际上没有什么东西禁止有几个管理数据库,并且允许没有自动黑暗选择)

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

./manage.py createsuperuser --database=intern_db

注意:db名称是配置中的别名。

在创建超级用户之前,您需要迁移到数据库。迁移

user@root:~$python manage.py migrate

这将在数据库中创建
auth_user
表。

您不使用
'django.db.backends.mysql'
作为数据库引擎的原因是什么?Python 3,尚未提供;)在运行
createsuperuser
之前,您不需要先
syncdb
吗?不,这可以明确地添加--database参数,但它太奇怪了:dbrouter指示读/写for user表在特定的数据库中,为什么默认情况下不使用它?