如何解决Django:如何输入外键来保存使用jQuery动态添加表单的表单集?
我正在使用modelFormsets渲染表单以编辑与页面上的项目相关的注释。 Note
模型具有一个与Project
模型的实例相关的外键,并且我使用Note
模型的现有实例(属于用户项目)渲染表单。正在查看。效果很好。
现在,我正在尝试使用jQuery添加新的客户端表单。我基本上修改了管理数据,并克隆了属于该表单的所有元素,修改了它们的属性,然后将它们插入适当的位置。这也起作用(在前端)。但是,提交时遇到了麻烦。
最初,我只修改了'#id_form-TOTAL_FORMS'
,但收到一个错误消息:'django.db.utils.IntegrityError: null value in column "project_id" violates not-null constraint.'
,DETAIL: Failing row contains (63,3,This is the third note,null)
,其中pk是63(我假设是DB分配的,因为我没有输入任何内容),3是注释编号,“这是第三个注释”是注释文本(我以表格形式写出),并且null当然是项目ID的外键
然后,我也尝试更新'#id_form-INITIAL_FORMS'
,但是,当我使用添加的新表单提交时,我的formset.is_valid()
方法返回false,并且formset.erros
包含[{},{},{'id': ['This field is required.]}]
,我认为这是新行的ID,因为正如我所说,我没有将它传递到任何地方。
我已经阅读了一些有关如何向表单集动态添加表单的问题,但是没有一个问题涉及模型具有外键的情况,因此我在这里有些迷惑。我不知道如何为将从刚刚创建的新表单创建的新实例传递“ project_id”,或者如果我不知道如何传递要创建的新实例的id(pk)会的。
这是我的观点(我知道排序可能不是最好的;我已经做了很多修改)。 请查看评论。
def editor(request,project_id):
if not Note.objects.filter(project_id=project_id).exists():
NoteFormset = modelformset_factory(Note,extra=1,fields=('note_text','note_number'),labels={'note_text': (''),'note_number': ('')},widgets={'caption_number':
NumberInput(attrs={'class': 'disabled','readonly': 'readonly'})})
else:
NoteFormset = modelformset_factory(Note,extra=0,'readonly': 'readonly'})})
formset = NoteFormset(queryset
=Note.objects.filter(project_id=project_id).order_by('note_number'))
if request.is_ajax and request.method == POST:
formset = NoteFormset(request.POST)
if formset.is_valid():
new_formset = formset.save()
serialized_formset = serializers.serialize('json',new_formset)
# This response is not actually being used by ajax
# because I don't need to update anything
# in the frontend after updating in the db. The user input stays there.
return JsonResponse({'new_formset': serialized_formset})
else:
print(formset.errors)
else:
return render(request,'NoteProj/editor.html',{'formset': formset})
这是呈现HTML的方式(相关部分)。 请查看评论。
<body>
<form method="POST" class="note-form">
<input type="hidden" name="csrfmiddlewaretoken" ...>
<div class="note-pane">
<!-- value="2" before adding the new form. Modified with jQuery-->
<input type="hidden" name="form-TOTAL_FORMS" value="3" id="id_form-TOTAL_FORMS">
<!-- value="2" before adding the new form. Modified with jQuery-->
<input type="hidden" name="form-INITIAL_FORMS" value="3" id="id_form-INITIAL_FORMS">
<input type="hidden" name="form-MIN_NUM_FORMS" value="0" id="id_form-MIN_NUM_FORMS">
<input type="hidden" name="form-MAX_NUM_FORMS" value="1000" id="id_form-MAX_NUM_FORMS">
<!-- First form -->
<p>
<textarea name="form-0-note_text" cols="70" rows="8" id="id_form-0-note_text">
This is the first note</textarea>
<!-- This button is added with jQuery to insert a new note after -->
<button id="ins-after-0" class="ins-note-after" type="button" value="ins-after-0">
</p>
<p>
<!-- Deleted styles for simplicity-->
<input type="number" name="form-0-note_number" value="1" class="disabled"
readonly="readonly" id="id_form-0-note_number">
<input type="hidden" name="form-0-id" value="60" id="id_form-0-id">
</p>
<!-- Second form -->
<p>
<textarea name="form-1-note_text" cols="70" rows="8" id="id_form-1-note_text">
This is the second note</textarea>
<!-- This button is added with jQuery to insert a new note after -->
<button id="ins-after-1" class="ins-note-after" type="button" value="ins-after-1">
</p>
<p>
<!-- Deleted styles for simplicity-->
<input type="number" name="form-1-note_number" value="2" class="disabled"
readonly="readonly" id="id_form-1-note_number">
<input type="hidden" name="form-1-id" value="61" id="id_form-1-id">
</p>
<!-- Third form. Added with jQuery -->
<p>
<textarea name="form-2-note_text" cols="70" rows="8" id="id_form-2-note_text">
This is the third note</textarea>
<!-- This button is added with jQuery to insert a new note after -->
<button id="ins-after-2" class="ins-note-after" type="button" value="ins-after-2">
</p>
<p>
<!-- Deleted styles for simplicity-->
<input type="number" name="form-2-note_number" value="3" class="disabled"
readonly="readonly" id="id_form-2-note_number">
<!-- HERE'S A PROBLEM. I don't know how to determine the pk value to input here -->
<input type="hidden" name="form-2-id" value="61" id="id_form-2-id">
</p>
</div>
</form>
</body>
总结。没有将要在数据库中创建的行的pk,我无法提交这个新创建的表单,而且我不知道如何确定它。此外,尽管当我不更新#id_form-INITIAL_FORMS
时会出现此问题,但我不知道如何输入project_id
,它将新行与特定项目相关联。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。