I have been trying since this morning and have read all the posts here and all over about accessing multiple databases in django with no avail. I m looking to access another database on the same server and I have included the databases in the settings.py with aliases. When I m trying to use the using() in the queryset I m getting an error that the Global name ‘Objectname’ does not exists. I m using postgresql 9.1 with django 1.4
Is there anything that I need to import for this to work? Its not working for me either in the console (python manage.py shell) or the views.
Here is the database setup from 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.
}
}
Here is the code from the console:
>>> MyModel.objects.using('app_data').all()
Traceback (most recent call last):
File "<console>", line 1, in <module>
NameError: name 'MyModel' is not defined
Here is the error from the view:
NameError at /myapp/view
global name 'MyModel' is not defined
Please help!
Edited: Just to give a little background I have 3 different apps running on one server with different databases and I wanted to access records across different apps just for viewing.
The 3 apps are app, ppp and testing and I m trying to access app and ppp data from my testing app.
I had to append the app paths for other apps to the wsgi file.
Then I could import the models from other apps
Then this allowed me to use the using() method in the views to get the records:
Hope this helps someone else.