1

デフォルトのデータベースにクエリ セットを使用できます。しかし、別のデータベースにクエリ セットを使用すると、例外がスローされます。

私のアプリケーションでは、2 つのデータベースを使用しています。sqlite と Mysql

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': 'abc.db',                      # Or path to database file if using sqlite3.
        'USER': '',                      # Not used with sqlite3.
        'PASSWORD': '',                  # Not used with sqlite3.
        'HOST': '',                      # Set to empty string for localhost. Not used with sqlite3.
        'PORT': '',                      # Set to empty string for default. Not used with sqlite3.
    },
      'second' : {
        'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': 'abc',                      # Or path to database file if using sqlite3.
        'USER': 'abcdb',                      # Not used with sqlite3.
        'PASSWORD': 'xxxxx',                  # Not used with sqlite3.
        'HOST': '',                      # Set to empty string for localhost. Not used with sqlite3.
        'PORT': '',                      # Set to empty string for default. Not used with sqlite3.
          }
}

最初のデータベースにクエリ セットを使用すると、例外がスローされません。2 番目のデータベースを使用している間は、スロー テーブルは使用できません。

TemplateSyntaxError at /abc/xyz/

Caught DatabaseError while rendering: no such table: second.tablename

Request Method:     GET
Request URL:    http://127.0.0.1:8000/xxx/yyyy/?q=abcd
Django Version:     1.3.1
Exception Type:     TemplateSyntaxError
Exception Value:    

Caught DatabaseError while rendering: no such table: second.tablename
4

2 に答える 2

1

Tom さん、返信ありがとうございます。2 つ目のデータベースを手動で試してみましたが、うまくいきました。

   $model_seconddb.modelname.objects.using('seconddatabasename').filter(name='xxx')

これは、すべてのテーブルが両方のデータベースにない場合に使用できます。

デフォルトのデータベースを使用する場合は、使用する (使用する) 必要はありません。デフォルトのデータベースから直接クエリを実行します。

于 2012-01-21T21:50:35.070 に答える