Ich mache eine Django-App zum Hochladen von Formularen. Ich weiß, dass ich es nicht auf eine normale Art mache, aber es funktioniert trotzdem. Mein Problem bezieht sich auf mehrere Datenbanken. Ich habe 3-Datenbanken in setting.py wie folgt definiert:Auswahl von verschiedenen DB in speichern() django
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'django',
'USER': 'root',
'PASSWORD': 'testing',
'HOST': 'localhost', # Or an IP Address that your DB is hosted on
'PORT': '3306',
},
'Firewall': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'Firewall_Policies',
'USER': 'root',
'PASSWORD': 'testing',
'HOST': 'localhost', # Or an IP Address that your DB is hosted on
'PORT': '3306',
},
'CES': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'CES_Policies',
'USER': 'root',
'PASSWORD': 'testing',
'HOST': '127.0.0.1', # Or an IP Address that your DB is hosted on
'PORT': '3306',
}
}
Das Modell der erforderlichen Klasse sieht wie folgt aus:
class ID_Table(models.Model):
FQDN = models.CharField(max_length=128, db_column="FQDN")
CES_Using = models.CharField(max_length=256, db_column="CES_Using")
MSISDN=models.CharField(max_length=16, db_column="MSISDN")
IPv4 = models.CharField(max_length=16, db_column="IPv4")
Unique_ID = models.CharField(max_length=64, db_column="Unique_ID")
Subscription = models.CharField(max_length=128, db_column="Subscription")
class Meta:
db_table='ID_Table'
Und forms.py Datei ist als:
class ID_TableForm(forms.ModelForm):
class Meta:
model = ID_Table
fields = ['FQDN', 'CES_Using', 'MSISDN', 'IPv4', 'Unique_ID', 'Subscription']
Und die Datei views.py ist in etwa wie folgt:
def add_table_page(request, table_specified):
request.session.set_expiry(2000)
if request.session.get('user_auth') == 'yes':
if request.method=="POST" and table_specified=='ID_Table.html':
img = ID_TableForm(request.POST, request.FILES)
img.FQDN = request.POST['FQDN']
img.CES_Using = request.POST['CES_Using']
img.MSISDN = request.POST['MSISDN']
img.IPv4 = request.POST['IPv4']
img.Unique_ID = request.POST['Unique_ID']
img.Subscription = request.POST['Subscription']
img.save(using='Firewall')
return render(request, table_specified)
return HttpResponseRedirect('../login')
Jetzt ist mein Problem, dass ich den USING-Befehl in der Funktion save() nicht auswählen kann. Es gibt mir die folgende Fehlermeldung:
save() got an unexpected keyword argument 'using'
Während, wenn ich das Wort der Verwendung entfernen und eine Tabelle mit demselben Namen in Standard-Datenbank erstellen sie die Daten erfolgreich einfügt. Ich möchte die Informationen in einer Tabelle speichern, die nicht in der Standarddatenbank enthalten ist.
Thankyou Soo viel Moses ... Ich bin dankbar. Es hat perfekt funktioniert :) –