简体   繁体   English

Django-检查数据库中是否存在变量集并对其进行处理?

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

So I have a Django App, where a CSV-File can be uploaded. 所以我有一个Django应用,可以在其中上传CSV文件。 My CSV-File has 9 columns that can be divided into two "datasets" where the first 5 columns need to be handled as one information and the other 4 need to be handled as another information. 我的CSV文件有9列,可以分为两个“数据集”,其中前5列需要作为一个信息处理,而其他4列需要作为另一种信息处理。 I cannot put the first 5 in one cell and the other ones in another cell. 我不能将前5个放在一个单元格中,而将其他5个放在另一个单元格中。 I would like to check whether or not the first dataset exists and if it does, process it. 我想检查第一个数据集是否存在,如果存在,请对其进行处理。 The same applies to the other dataset. 其他数据集也是如此。 And if both datasets do not exist already it should just update the Database with get_or_create. 并且如果两个数据集都不存在,则应使用get_or_create更新数据库。

Here is my views.py idea 这是我的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}
    )

It should look something like this. 它看起来应该像这样。 How can I make this work. 我该如何进行这项工作。 It was just an idea with filter() and exists() but is there a Python way to do something like this? 这只是使用filter()和exist()的一个主意,但是有没有Python的方式可以做这样的事情? Any help would be appreciated. 任何帮助,将不胜感激。

Currently, you are trying to recursively iterate through reader three times. 当前,您正在尝试递归遍历reader三遍。 That's not possible, because it is an Iterator , not a list . 这是不可能的,因为它是一个Iterator ,而不是list Anyway, you only need to do it once and then work on that particular line, before skipping to the next. 无论如何,在跳到下一行之前,您只需要执行一次,然后在该特定行上工作即可。

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