首页 > 其他 > 详细

django modelformse批量编辑 查询学生班级成绩

时间:2018-11-20 22:38:27      阅读:220      评论:0      收藏:0      [点我收藏+]

复杂版

将字段设置联合唯一

class Meta:
unique_together=["student","classstudyrecord"]
技术分享图片
 1 class ClassStudyRecordView(View):
 2     def get(self,request):
 3         ClassStudyRecordlist=ClassStudyRecord.objects.all()
 4         return render(request,"ClassStudyRecord.html",{"ClassStudyRecordlist":ClassStudyRecordlist})
 5 
 6 
 7 
 8     def post(self, request):
 9         print(request.POST)
10         func_st = request.POST.get("action")
11         num = request.POST.getlist("selected_pk_list")
12 
13         print("num", num)
14         if not hasattr(self, func_st):
15             return HttpResponse("非法输入")
16         else:
17             func = getattr(self, func_st)
18             queryset = ClassStudyRecord.objects.filter(pk__in=num)
19 
20             print("queryset", queryset)
21             ret = func(request, queryset)
22             if ret:
23                 return ret
24             return redirect(request.path)
25         #批量删除
26     def patch_delete(self, request, queryset):
27         queryset.delete()
28         #批量查询,利用跨表查询查询创建字段,为了避免添加重复使用联合唯一
29     def patch_choice(self,request,queryset):
30         try:
31             for i in queryset:
32                 for item in i.class_obj.student_set.all():
33                     StudentStudyRecord.objects.create(student=item,classstudyrecord=i)
34         except Exception as e:
35             pass
views.py

跳转页面进行编辑,通过获取form的值构成列表循环录入成绩

技术分享图片
 1 def buhui(request,id):
 2 
 3     if request.method=="GET":
 4         ret=StudentStudyRecord.objects.filter(classstudyrecord_id=id)
 5         return  render(request,"student_std.html",locals())
 6     else:
 7 
 8         user = request.POST.getlist("user")
 9         homework = request.POST.getlist("homework")
10         for a,item in enumerate(ClassStudyRecord.objects.filter(pk=id).first().class_obj.student_set.all()):
11             print(a,item)
12             StudentStudyRecord.objects.filter(student=item).update(score=user[a],homework_note=homework[a])
13         return redirect(request.path)
views.py录入成绩
技术分享图片
 1 <table>
 2     <form action="" method="post">
 3         {% csrf_token %}
 4         <thead>
 5         <tr>
 6 
 7             <th>编号</th>
 8             <th>姓名</th>
 9             <th>考勤</th>
10             <th>成绩</th>
11             <th>批语</th>
12 
13         </tr>
14         </thead>
15         <tbody>
16 
17         {% for consult_record in ret %}
18             <tr>
19                 <td>{{ forloop.counter }}</td>
20                 <td>{{ consult_record.student }}</td>
21                 <td>{{ consult_record.get_record_display }}</td>
22                 <td><select name="user" id="">
23                     {% for foo in consult_record.score_choices %}
24                         #进行判断,若是存在值添加selected让其显示,没有显示默认
25                         {% if consult_record.score == foo.0 %}
26                             <option selected value="{{ foo.0 }}">{{ foo.1 }}</option>
27                         {% else %}
28                             <option value="{{ foo.0 }}">{{ foo.1 }}</option>
29                         {% endif %}
30                     {% endfor %}
31                     <option value=""></option>
32                 </select>
33                 </td>
34                 <td><input type="text" name="homework" value="{{ consult_record.homework_note }}"></td>
35             </tr>
36         {% endfor %}
37 
38 
39         </tbody>
40         <button class="btn btn-default">提交</button>
41 
42 
43     </form>
44 </table>
前端.html

 

modelformset 简单版

1.首先引入模块

from django.forms.models import modelformset_factory

2.

后端

技术分享图片
 1 from django.forms.models import modelformset_factory
 2 
 3 class StudentStudyRecordModelForm(forms.ModelForm):
 4     class Meta:
 5         model=StudentStudyRecord
 6         fields=["score","homework_note"]
 7 
 8 class RecordScoreView(View):
 9 
10     def get(self, request,class_study_record_id):
11 #modelformset_factory()里面需要2个变量,model和modelform,extra默认为1,显示添加
12         model_formset_cls=modelformset_factory(model=StudentStudyRecord,form=StudentStudyRecordModelForm,extra=0)
13         queryset = StudentStudyRecord.objects.filter(classstudyrecord=class_study_record_id)
14         formset = model_formset_cls(queryset=queryset)
15         return render(request,"student/record_score.html",locals())
16 
17     def post(self, request,class_study_record_id):
18         model_formset_cls = modelformset_factory(model=StudentStudyRecord, form=StudentStudyRecordModelForm, extra=0)
19         queryset = StudentStudyRecord.objects.filter(classstudyrecord=class_study_record_id)
20         print("request.POST",request.POST)
21         formset=model_formset_cls(request.POST)
22         if formset.is_valid():
23             formset.save()
24 
25         print(formset.errors)
26 
27         return redirect(request.path)
modelformset 批量编辑

3.

前端

技术分享图片
 1 <hr>
 2 
 3 
 4 
 5  <div class="panel panel-default">
 6         <div class="panel-heading">学习记录</div>
 7         <div class="panel-body">
 8             <div style="width: 680px;margin: 0 auto;">
 9                 <form method="post" action="">
10                     {% csrf_token %}
11                 #这个必须要写
12                     {{ formset.management_form }}
13 
14                     <table class="table table-bordered">
15                         <thead>
16                         <tr>
17                             <th>姓名</th>
18                             <th>考勤</th>
19                             <th>作业成绩</th>
20                             <th>作业评语</th>
21                         </tr>
22                         </thead>
23                         <tbody>
24                         {% for form in formset %}
25                             <tr>
26                                 {{ form.id }}
27                                 #form.instance.为显示界面,不添加编辑页面
28                                 <td>{{ form.instance.student }}</td>
29                                 <td>{{ form.instance.get_record_display }} </td>
30                                 <td>{{ form.score }} </td>
31                                 <td>{{ form.homework_note }}</td>
32                             </tr>
33                         {% endfor %}
34                         </tbody>
35                     </table>
36                     <input type="submit" value="保存">
37                 </form>
38             </div>
39         </div>
40     </div>
41 <hr>
前端.html

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 




 

django modelformse批量编辑 查询学生班级成绩

原文:https://www.cnblogs.com/zhangqing979797/p/9991902.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!