modelformset
Posted xingkongzhizhu
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了modelformset相关的知识,希望对你有一定的参考价值。
class StudyRecordDeialView(View):
def get(self, request, class_record_id):
class_record_obj = models.ClassStudyRecord.objects.get(pk=class_record_id)
print(class_record_obj)
all_study_recored = models.StudentStudyRecord.objects.filter(
classstudyrecord=class_record_obj,
)
form_set_obj = modelformset_factory(model=models.StudentStudyRecord,form=StudyRecordDeialModelForm,extra=0)
formset = form_set_obj(queryset=all_study_recored)
return render(request, ‘student/study_record_detail.html‘,‘formset‘: formset)
def post(self, request, class_record_id):
class_record_obj = models.ClassStudyRecord.objects.get(pk=class_record_id)
all_study_recored = models.StudentStudyRecord.objects.filter(
classstudyrecord=class_record_obj,
)
form_set_obj = modelformset_factory(model=models.StudentStudyRecord, form=StudyRecordDeialModelForm, extra=0)
formset = form_set_obj(request.POST)
if formset.is_valid():
formset.save()
else:
print(formset.errors)
return redirect(reverse(‘study_decord‘,args=(class_record_id,)))
formset.management_form <!-- 使用modelformset的时候这句话一定要加在前端页面里,固定的昂 -->
以上是关于modelformset的主要内容,如果未能解决你的问题,请参考以下文章