簡體   English   中英

Django-檢查數據庫中是否存在變量集並對其進行處理?

[英]Django - Check if a variable set exists in the Database and process it if it does?

所以我有一個Django應用,可以在其中上傳CSV文件。 我的CSV文件有9列,可以分為兩個“數據集”,其中前5列需要作為一個信息處理,而其他4列需要作為另一種信息處理。 我不能將前5個放在一個單元格中,而將其他5個放在另一個單元格中。 我想檢查第一個數據集是否存在,如果存在,請對其進行處理。 其他數據集也是如此。 並且如果兩個數據集都不存在,則應使用get_or_create更新數據庫。

這是我的views.py想法

    def import_csv(request):
    if request.method == "POST":
        with open('C:/Users/admin/Desktop/djangoexcel/b.csv') as file:
            reader = csv.reader(file)
            for row in reader:
                var = CSV_File4.objects.filter(
                    attr1=row[0], attr2=row[1], attr3=row[2], attr4=row[3], attr5=row[4],
                )
                if var.exists():
                    TemplateResponse(request, "documents/replace_entry.html", {'var' : var})
                else:
                    for row in reader:
                        switch = CSV_File4.objects.filter(
                            attr6=row[5], attr7=row[6], attr8=row[7], attr9=row[8]
                        )
                        if var2.exists():
                            TemplateResponse(request, "documents/replace_entry.html", {'var2' : var2})
                        else:
                            for row in reader:
                                _, p = CSV_File4.objects.get_or_create(
                                    attr1=row[0], attr2=row[1], attr3=row[2], attr4=row[3], attr5=row[4],
                                    attr6=row[5], attr7=row[6], attr8=row[7], attr9=row[8]
                                )


            return redirect('documents:index')
    form = UploadFileForm()
    return render(
        request, "documents/csv_upload.html", {"form": form}
    )

它看起來應該像這樣。 我該如何進行這項工作。 這只是使用filter()和exist()的一個主意,但是有沒有Python的方式可以做這樣的事情? 任何幫助,將不勝感激。

當前,您正在嘗試遞歸遍歷reader三遍。 這是不可能的,因為它是一個Iterator ,而不是list 無論如何,在跳到下一行之前,您只需要執行一次,然后在該特定行上工作即可。

def import_csv(request):
    if request.method == "POST":
        with open('C:/Users/admin/Desktop/djangoexcel/b.csv') as file:
            reader = csv.reader(file)
            for row in reader:
                ds1 = CSV_File4.objects.filter(
                    attr1=row[0], attr2=row[1], attr3=row[2], attr4=row[3], attr5=row[4],
                ).exists()
                ds2 = CSV_File4.objects.filter(
                    attr6=row[5], attr7=row[6], attr8=row[7], attr9=row[8]
                ).exists()

                if ds1:
                    pass  # Process first dataset
                if ds2:
                    pass  # Process second dataset
                if not (ds1 and ds2):
                    _, p = CSV_File4.objects.get_or_create(
                        attr1=row[0], attr2=row[1], attr3=row[2], attr4=row[3], attr5=row[4],
                        attr6=row[5], attr7=row[6], attr8=row[7], attr9=row[8]
                    )

            return redirect('documents:index')

    return render(
        request, "documents/csv_upload.html", {"form": UploadFileForm()}
    )

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM