私は今朝から試してみましたが、ここにあるすべての投稿と、django で複数のデータベースにアクセスすることに関するすべての記事を読みましたが、役に立ちませんでした。同じサーバー上の別のデータベースにアクセスしようとしていますが、エイリアスを使用して settings.py にデータベースを含めました。クエリセットで using() を使用しようとすると、グローバル名 'Objectname' が存在しないというエラーが表示されます。私はdjango 1.4でpostgresql 9.1を使用しています
これを機能させるためにインポートする必要があるものはありますか? コンソール(python manage.py shell)でもビューでも機能しません。
settings.py からのデータベースのセットアップは次のとおりです。
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'testing', # Or path to database file if using sqlite3.
'USER': 'xxxx', # Not used with sqlite3.
'PASSWORD': 'xxxx', # Not used with sqlite3.
'HOST': 'localhost', # Set to empty string for localhost. Not used with sqlite3.
'PORT': 'xxxx', # Set to empty string for default. Not used with sqlite3.
},
'app_data': {
'ENGINE': 'django.db.backends.postgresql_psycopg2', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'applications', # Or path to database file if using sqlite3.
'USER': 'xxxx', # Not used with sqlite3.
'PASSWORD': 'xxxx', # Not used with sqlite3.
'HOST': 'localhost', # Set to empty string for localhost. Not used with sqlite3.
'PORT': 'xxxx', # Set to empty string for default. Not used with sqlite3.
},
'ppp_data': {
'ENGINE': 'django.db.backends.postgresql_psycopg2', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'platform', # Or path to database file if using sqlite3.
'USER': 'xxxx', # Not used with sqlite3.
'PASSWORD': 'xxxx', # Not used with sqlite3.
'HOST': 'localhost', # Set to empty string for localhost. Not used with sqlite3.
'PORT': 'xxxx', # Set to empty string for default. Not used with sqlite3.
}
}
コンソールからのコードは次のとおりです。
>>> MyModel.objects.using('app_data').all()
Traceback (most recent call last):
File "<console>", line 1, in <module>
NameError: name 'MyModel' is not defined
ビューからのエラーは次のとおりです。
NameError at /myapp/view
global name 'MyModel' is not defined
助けてください!
編集済み:少し背景を説明するために、1つのサーバーで異なるデータベースを使用して3つの異なるアプリを実行しており、表示するためだけに異なるアプリのレコードにアクセスしたかった. 3 つのアプリは app、ppp、testing で、testing アプリからアプリと ppp データにアクセスしようとしています。