1.
#引用modelformsetfrom django.forms.models import modelformset_factory#必须继承forms.ModelForm!class StudentStudyRecordModelForm(forms.ModelForm): class Meta:#需要建立关联的表StudentStudyRecord model = StudentStudyRecord#score", "homework_note 显示的字段 fields = ["score", "homework_note"]
2.
class RecordScoreView(View): def get(self, request, class_study_record_id): # modelformset_factory固定的写法 ,model=StudentStudyRecord这是需要关联的表名,form后边是我们上边的新建类名 , extra=0 不加它默认数据多一行 model_formset_cls = modelformset_factory(model=StudentStudyRecord, form=StudentStudyRecordModelForm, extra=0) queryset = StudentStudyRecord.objects.filter(classstudyrecord=class_study_record_id) print(class_study_record_id) #2 第2天的课程 print("queryset",queryset) #查询第2天课程的学生记录 formset = model_formset_cls(queryset=queryset) return render(request, "record_score.html", locals()) def post(self, request, class_study_record_id): model_formset_cls = modelformset_factory(model=StudentStudyRecord, form=StudentStudyRecordModelForm, extra=0) queryset = StudentStudyRecord.objects.filter(classstudyrecord=class_study_record_id) # print("request.POST", request.POST) formset = model_formset_cls(request.POST) #??????????????????????????????// print("formset",formset) print(123456789) #判断是否通过校验 if formset.is_valid(): formset.save() # print(formset.errors) return redirect(request.path)
3.
#record_score.htmlTitle 录入{ { class_study_record_obj.class_obj }}day{ { class_study_record_obj.day_num }}成绩
学习记录