0
訪問多個數據庫從今天上午開始,我一直在嘗試,並且已經閱讀了所有的帖子,並且都在關於訪問django中的多個數據庫而無濟於事。我正在尋找訪問同一臺服務器上的另一個數據庫,我已經將這些數據庫包含在settings.py中,並帶有別名。當我嘗試在查詢集中使用using()時,出現全局名稱'Objectname'不存在的錯誤。我使用postgresql 9.1與Django 1.4在Django中使用()
有什麼,我需要導入這個工作?它不在我的控制檯(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
P租賃幫助!
編輯︰只是爲了給一個小背景我有3個不同的應用程序運行在一個服務器上與不同的數據庫,我想跨不同的應用程序訪問記錄只是爲了查看。 3個應用程序是應用程序,ppp和測試,我試圖從我的測試應用程序訪問應用程序和ppp數據。
看起來您在視圖中缺少對「MyModel」的導入。 –
從不同模型的不同數據庫中導入「MyModel」的語法是什麼? – us1415
我已經嘗試'從app.MyModel.models導入*'在視圖中的測試應用程序,但它也不工作。 – us1415