Python 使用django创建没有ID的重复条目

Python 使用django创建没有ID的重复条目,python,django,Python,Django,我正在制作一个django仪表板,用户可以在其中上传他们的经验证书 问题在于,certification部分使用空ID向django视图发送重复条目,而视图中的逻辑在数据库中生成重复条目 我发现的问题是表单正在发送没有ID的重复Entri certificate.html <form method="POST" enctype="multipart/form-data"> {% csrf_token %} {% for c in cert %}

我正在制作一个django仪表板,用户可以在其中上传他们的经验证书

问题在于,certification部分使用空ID向django视图发送重复条目,而视图中的逻辑在数据库中生成重复条目

我发现的问题是表单正在发送没有ID的重复Entri

certificate.html

<form method="POST" enctype="multipart/form-data">
         {% csrf_token %}
         {% for c in cert %}

                    <input type="text" name="id" value="{{c.id}}">
                           <label>Month</label>
                           <input type="text" class="form-control" id="field-value" name = "month" value = "{{c.month}}">

                              <label>Year</label>
                              <input type="text" class="form-control" id="field-value" name = "year" value = "{{c.year}}">
                                <label>Company</label>
                                  <select class="form-control" id="field-value" name = "company">
                                 {% if c.company %}
                                <option value = "{{c.company}}">{{c.company}}</option>
                                {% endif %}
                                <option value="Microsoft">Microsoft</option>
                                <option value="TEDx">TEDx</option>
                                <option value="Business Standard">Business Standard</option>
                                <option value="EF Standard English Test">EF Standard English Test</option>
                               <option value="Open2Study">Open2Study</option>
                               <option value="eMarketing Institute">eMarketing Institute</option>
                               <option value="Amazon">Amazon</option>
                               <option value="Airbnb">Airbnb</option>
                               <option value="Adobe">Adobe</option>
                               <option value="Paypal">Paypal</option>
                               <option value="Intel">Intel</option>
                               <option value="eBay">eBay</option>
                               <option value="Beats">Beats</option>
                              </select>

                              <label>Title</label>
                              <input type="text" class="form-control" id="field-value" name = "title" value="{{c.title}}">


                             <label>Certificate</label>
                             <input type="file" class="form-control" id="field-value" name = "img" value="{{c.img}}"> 
                             {% endfor %}
                             <button type="submit" name = "cert-sec" class="btn btn-info">Submit</button>
                             <button type="reset" class="btn btn-dark">Reset</button>
                                </form>

请立即帮助。谢谢。

假设可以通过使用标题、img和公司来定义唯一性,您可以这样做:

elif 'cert-sec' in request.POST:
            idi = request.POST.getlist('id')
            month = request.POST.getlist('month')
            year = request.POST.getlist('year')
            company = request.POST.getlist('company')
            title = request.POST.getlist('title')
            img = request.POST.getlist('img')
            print(idi)
            print(len(title))
            comp_list = []
            for i in range(len(title)):
                combined_str = company[i]+title[i]+img[i]
                if combined_str in comp_list:
                    continue
                comp_list.append(combined_str)
                certi = Certification(month = month[i], year = year[i], company = company[i], title = title[i], img=img[i])
                if idi[i] is None or idi[i] is '':
                    certi.save()
                elif idi[i] is not None and certi in cert:
                    cer = Certification.objects.get(id = idi[i])
                    #print(edi.id)
                    cer.month = month[i]
                    cer.year = year[i]
                    cer.company = company[i]
                    cer.title = title[i]
                    cer.img = img[i]
            return redirect('homepage')

我发现错误在我的原始代码中。我有一个与我之前创建的同名的隐藏输入字段,但忘记删除。“请立即获得帮助。”=>抱歉,我们不是免费的支持服务。
elif 'cert-sec' in request.POST:
            idi = request.POST.getlist('id')
            month = request.POST.getlist('month')
            year = request.POST.getlist('year')
            company = request.POST.getlist('company')
            title = request.POST.getlist('title')
            img = request.POST.getlist('img')
            print(idi)
            print(len(title))
            comp_list = []
            for i in range(len(title)):
                combined_str = company[i]+title[i]+img[i]
                if combined_str in comp_list:
                    continue
                comp_list.append(combined_str)
                certi = Certification(month = month[i], year = year[i], company = company[i], title = title[i], img=img[i])
                if idi[i] is None or idi[i] is '':
                    certi.save()
                elif idi[i] is not None and certi in cert:
                    cer = Certification.objects.get(id = idi[i])
                    #print(edi.id)
                    cer.month = month[i]
                    cer.year = year[i]
                    cer.company = company[i]
                    cer.title = title[i]
                    cer.img = img[i]
            return redirect('homepage')