デフォルトのデータベースにクエリセットを使用できます。 しかし、別のデータベースにクエリセットを使用すると、例外がスローされます。djangoの複数のデータベースに対してクエリセットを使用できません
私のアプリケーションでは、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
テーブルがデータベースに存在していることを確認してください。 – dm03514
ルーター(https://docs.djangoproject.com/en/dev/topics/db/multi-db/#using-routers)を作成しましたか?おそらくテーブルはデータベースのうちの1つにしかありません(照会しようとするとDjangoがスローするのではなく)。 – Tom
database1には異なるテーブルがあり、database2には異なるテーブルがあります。 – sreekanth