Django-重新上传和更新CSV文件到数据库

时间:2018-12-06 10:44:49

标签: python django database

我想通过重新上传更改的CSV文件来更新Django SQLite3数据库。

这是我的const dog = { age: 5, growOneYear: function () { this.age += 1; } }; dog.growOneYear(); console.log(dog.age); // 6 function invokeTwice(cb) { cb(); cb(); } // Bind the method to the desired object // Inside `invokeTwice()`, `cb` is the function `dog.growOneYear` with // `this` pointing to `dog` ------+ // v invokeTwice(dog.growOneYear.bind(dog)); console.log(dog.age); // 8

models.py

我的class CSV_File8(models.Model): gebäudebereich = models.CharField(max_length=100) gebäudenr = models.CharField(max_length=100) ebene = models.CharField(max_length=100) raum = models.CharField(max_length=100) dose = models.CharField(max_length=100) switch_ip = models.CharField(max_length=100) switch_port = models.CharField(max_length=100) datum = models.CharField(max_length=100) akteur = models.CharField(max_length=100) class Meta: unique_together = (("gebäudebereich", "gebäudenr", "ebene", "raum", "dose"), ("switch_ip", "switch_port", "datum", "akteur"))

views.py

我收到错误消息class UploadFileForm(forms.Form): file = forms.FileField() def import_csv(request): if request.method == "POST": with open('C:/Users/admin/Desktop/djangoexcel/b.csv') as file: reader = csv.reader(file) id = 0 for row in reader: id += 1 # _, p = CSV_File8.objects.get_or_create(id = id, # gebäudebereich=row[0], gebäudenr=row[1], ebene=row[2], raum=row[3], dose=row[4], # switch_ip=row[5], switch_port=row[6], datum=row[7], akteur=row[8] # ) ds1 = CSV_File8.objects.filter(id = id, gebäudebereich=row[0], gebäudenr=row[1], ebene=row[2], raum=row[3], dose=row[4] ).values().exists() ds2 = CSV_File8.objects.filter(id = id, switch_ip=row[5], switch_port=row[6], datum=row[7], akteur=row[8] ).values().exists() if ds1 and id == id: pass if ds2 and id == id: pass if ds1 == False and id == id: _, ds1 = CSV_File8.objects.values().update(id = id, gebäudebereich=row[0], gebäudenr=row[1], ebene=row[2], raum=row[3], dose=row[4] ) if ds2 == False and id == id: _, ds2 = CSV_File8.objects.values().update(id = id, switch_ip=row[5], switch_port=row[6], datum=row[7], akteur=row[8] ) if ds1 and ds2 == False and id != id: _, ds2 = CSV_File8.objects.values().update(id=id, switch_ip=row[5], switch_port=row[6], datum=row[7], akteur=row[8] ) return redirect('documents:index') # wert = CSV_File3.objects.values_list('dose') # for i in wert: # if wert[i] == wert[i + 1]: # #return render(request, 'documents/replace_entry.html', {'p' : p}) # return replace_entry(request, p) form = UploadFileForm() return render( request, "documents/csv_upload.html", {"form": form} ) 。在这种情况下,我更改了CSV文件的第3行,并且ds1为False,这是正确的。现在,它应该只更新值并将其保存到数据库。但是,这给了我错误。我知道这是因为在此特定行中已经有一个唯一值,但是我认为上面的代码通过仅更新值来处理这种情况。如果您需要更多信息,请告诉我。 任何帮助将不胜感激。

0 个答案:

没有答案