/ / Zugriff auf mehrere Datenbanken in Django mit using () - django, django-models

Zugriff auf mehrere Datenbanken in Django mit () - django, django-models

Ich habe es seit heute Morgen versucht und habeLesen Sie alle Beiträge hier und überall über den Zugriff auf mehrere Datenbanken in Django ohne Erfolg. Ich suche nach einer anderen Datenbank auf demselben Server und habe die Datenbanken in die settings.py mit Aliases eingefügt. Wenn ich versuche, using () im Queryset zu verwenden, erhalte ich eine Fehlermeldung, dass der globale Name "Objectname" nicht vorhanden ist. Ich verwende Postgresql 9.1 mit Django 1.4

Gibt es etwas, das ich importieren muss, damit dies funktioniert? Es funktioniert weder in der Konsole (python manage.py shell) noch in den Views.

Hier ist das Datenbank-Setup von 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.
}

}

Hier ist der Code von der Konsole:

>>> MyModel.objects.using("app_data").all()
Traceback (most recent call last):
File "<console>", line 1, in <module>
NameError: name "MyModel" is not defined

Hier ist der Fehler aus der Sicht:

NameError at /myapp/view
global name "MyModel" is not defined

Bitte helfen Sie!

Bearbeitet: Um ein wenig Hintergrundwissen zu geben, habe ich 3 verschiedene Apps, die auf einem Server mit unterschiedlichen Datenbanken laufen, und ich wollte auf Datensätze in verschiedenen Apps zugreifen, nur um sie anzusehen. Die 3 Apps sind App, PPP und Testing. Ich versuche, auf App- und PPP-Daten von meiner Test-App zuzugreifen.

Antworten:

0 für die Antwort № 1

Ich musste die App-Pfade für andere Apps an die Wsgi-Datei anhängen.

import os, sys
sys.path.append("/opt/www/test")
sys.path.append("/opt/www/app")
sys.path.append("/opt/www/ppp")

Dann konnte ich die Modelle aus anderen Apps importieren

from pp_app.myapp.models import MyModel
from pp_ppp.myapp.models import MyModel

Dann konnte ich die using () - Methode in den Ansichten verwenden, um die Datensätze abzurufen:

data1 = MyModel.objects.using("app_data").all()
data2 = MyModel.objects.using("ppp_data").all()

Hoffe das hilft jemand anderem.