Django无法在“默认"以外的其他数据库中创建超级用户 [英] Django fails to create superuser in other db than 'default'

查看:218
本文介绍了Django无法在“默认"以外的其他数据库中创建超级用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是错误还是我错了? 我正在创建超级用户,但是django希望在错误的db中创建一个表,尽管我的路由器似乎可以正常工作:

Is it a bug or am I wrong ? I am at the step to create a superuser, but django want a table in wrong db despite my router seems to work :

settings.py

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

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" ...

As you can see, it looks for 'cartons.auth_user' that doesn't exist (it should be 'django_cartons' aliased by 'intern_db' instead) However, my router is called and return the right result as we see "READ auth" and "TRUE" in the command output...

有什么想法吗?

推荐答案

问题是系统有些损坏:它尊重某些任务的配置,但不尊重其他任务的配置(输出中的第2个"TRUE"),但是不适合其他用户,请使用默认值.

The thing is the system is somewhat broken : it respects the config for some task but not for others (the 2 first "TRUE" in the output) but it doesn't for other and use default.

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

This is perhaps intended even if weird (actually nothing forbid to have several admin db, and that permits not have automatic dark choices).

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

Actually to create the SU in another db, and for any usage of these command, you must pass the database where you want to create it explicitly :

./manage.py createsuperuser --database=intern_db

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

NOTE : the db name is the alias in the config.

这篇关于Django无法在“默认"以外的其他数据库中创建超级用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆