在内联表单集中编辑记录时,在提交后也会为现有对象创建新对象(重复(。如何预防?
我删除了模型中的所有对象,并在检查 is_valid(( 后重新插入它。一切正常,但媒体文件无法保存。
views.py
def edit_frm(request,id):
ob = DynamicMain.objects.get(id=id)
if request.method == 'POST':
formsety = DocumentsUploadFormset(request.POST,request.FILES, prefix='formsety')
formsetz = DocumentsDownloadFormset(request.POST,request.FILES, prefix='formsetz')
if formsety.is_valid() and formsetz.is_valid():
del_up_ob = DocumentsUpload.objects.filter(title=ob).delete() #delete all objects
del_dwn_ob = DocumentsDownload.objects.filter(title=ob).delete()
for i in formsety:
name = i.cleaned_data.get('name')
mandatory = i.cleaned_data.get('mandatory')
types_doc = i.cleaned_data.get('types_doc')
location = i.cleaned_data.get('location')
DocumentsUpload(
title=ob,
name=name,
mandatory=mandatory,
types_doc=types_doc,
location=location,
).save()
for i in formsetz:
name = i.cleaned_data.get('name')
mandatory = i.cleaned_data.get('mandatory')
types_doc = i.cleaned_data.get('types_doc')
location = i.cleaned_data.get('location')
DocumentsDownload(
title=ob,
name=name,
mandatory=mandatory,
types_doc=types_doc,
location=location,
).save()
return redirect('app-admin:home')
uploadformset = inlineformset_factory(DynamicMain,
DocumentsUpload,extra=0,fields=('name','mandatory'))
downloadformset = inlineformset_factory(DynamicMain,
DocumentsDownload,extra=0, fields=('name','mandatory','types_doc'))
formsety = uploadformset(prefix='formsety',instance=ob)
formsetz = downloadformset(prefix='formsetz',instance=ob)
context = {
'formsety' : formsety,
'formsetz' : formsetz,
}
return render(request,'app/edit_form.html', context)
edit_form.html
<h3>Upload</h3>
<br>
<div class="content formElements add_item_container six formsety hide_add_button">
<table cellpadding="0" cellspacing="0" class="table">
<tr>
<th>Name</th>
<th>Mandatory</th>
</tr>
{% for f in formsety.forms %}
<tr class="form_up">
<td>
<span class="p5name1">
{{ f.name }}
</span>
</td>
<td>
<span class="p5name1">
{{ f.mandatory }}
</span>
</td>
<td>{% if f.instance.pk %}{{ f.DELETE }}{% endif %}</td>
</tr>
{% endfor %}
</table>
{{ formsety.management_form }}
</div>
<hr> -->
<h3>Download</h3>
<hr>
<br>
<div class="content formElements add_item_container six formsetz hide_add_button">
<table cellpadding="0" cellspacing="0" class="table">
<tr>
<th>Name</th>
<th>Mandatory</th>
<th>Type doc</th>
</tr>
{% for f in formsetz.forms %}
<tr class="form_dwn">
<td>
<span class="p5name1">
{{ f.name }}
</span>
</td>
<td>
<span class="p5name1">
{{ f.mandatory }}
</span>
</td>
<td>
<span class="p5name1">
{{ f.types_doc }}
</span>
</td>
<td>{% if f.instance.pk %}{{ f.DELETE }}{% endif %}</td>
</tr>
{% endfor %}
</table>
{{ formsetz.management_form }}
</div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript" src="{% static 'admin/js/jquery.formset.js' %}"></script>
<script src="{% static 'admin/js/custom.min.js' %}"></script>
<script type="text/javascript">
$(document).ready(function(){
$('.formsetx table tr.form_set_rowp5').formset({
prefix: '{{ formsetx.prefix }}',
formCssClass: 'dynamic-formset1',
});
})
$(document).ready(function(){
$('.formsety table tr.form_up').formset({
prefix: '{{ formsety.prefix }}',
formCssClass: 'dynamic-formset1',
});
})
$(document).ready(function(){
$('.formsetz table tr.form_dwn').formset({
prefix: '{{ formsetz.prefix }}',
formCssClass: 'dynamic-formset1',
});
})
</script>
我想在不创建现有数据副本的情况下更新数据。
可能需要在此处添加实例:
formsety = DocumentsUploadFormset(request.POST,request.FILES, prefix='formsety', instance = ob)
formsetz = DocumentsDownloadFormset(request.POST,request.FILES, prefix='formsetz', instance = ob)