Django无法在其他数据库中创建超级用户而不是默认'

时间:2015-02-11 11:59:22

标签: python django multi-database

这是一个错误还是我错了? 我正在创建一个超级用户的步骤,但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"在命令输出中......

任何想法?

2 个答案:

答案 0 :(得分:3)

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

即使很奇怪,这也许是有意的(实际上并没有禁止有几个admin db,并且不允许自动选择黑暗)。

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

./manage.py createsuperuser --database=intern_db

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

答案 1 :(得分:0)

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

user@root:~$python manage.py migrate

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