django modelformse批量编辑 查询学生班级成绩
复习先知
- 关于三张表的编辑学生成绩
在跨表查询的对象查询种,只能通过找到两张表的关联的对象,
进行跨表,就是在一对多或多对多的模型找到他们俩的class_id或student_id
在关联时,会通过他们找到所要查询的id,或字段,进而找到他们的字段
进而查询出对象,这个表的对象, .出来
在这种跨三表的途中,编辑跟添加同理.都是通过过
过滤出你要找到学生的对象 学生成绩关联班级成绩 班级成绩关联班级 班级关联学生
这就是这张表的精髓- 关于modelformset 可以省去复杂跨表的骚操作
也可以一次性多删除
第一步
引入from django.forms.models import modelformset_factory- modelformset_factory(含2个变量,还有一个默认为1)
model() modelform() extra=1
1.实例化对象
2.找到点击的对象 queryset对象
3.实例化对象的方法(queryset=queryset)
返回给页面 自动给你
页面: 一样 但需要标注{{formset.management_form}}- 在编辑时候,
1.实例化对象modelformset_factory()
2.formset(request.post)
3.if formset.is_valid() form的通病
4.formset.save()
复杂版
将字段设置联合唯一
- class Meta:
unique_together=["student","classstudyrecord"]
- class ClassStudyRecordView(View):
- def get(self,request):
- ClassStudyRecordlist=ClassStudyRecord.objects.all()
- return render(request,"ClassStudyRecord.html",{"ClassStudyRecordlist":ClassStudyRecordlist})
- def post(self, request):
- print(request.POST)
- func_st = request.POST.get("action")
- num = request.POST.getlist("selected_pk_list")
- print("num", num)
- if not hasattr(self, func_st):
- return HttpResponse("非法输入")
- else:
- func = getattr(self, func_st)
- queryset = ClassStudyRecord.objects.filter(pk__in=num)
- print("queryset", queryset)
- ret = func(request, queryset)
- if ret:
- return ret
- return redirect(request.path)
- #批量删除
- def patch_delete(self, request, queryset):
- queryset.delete()
- #批量查询,利用跨表查询查询创建字段,为了避免添加重复使用联合唯一
- def patch_choice(self,request,queryset):
- try:
- for i in queryset:
- for item in i.class_obj.student_set.all():
- StudentStudyRecord.objects.create(student=item,classstudyrecord=i)
- except Exception as e:
- pass
views.py
跳转页面进行编辑,通过获取form的值构成列表循环录入成绩
- def buhui(request,id):
- if request.method=="GET":
- ret=StudentStudyRecord.objects.filter(classstudyrecord_id=id)
- return render(request,"student_std.html",locals())
- else:
- user = request.POST.getlist("user")
- homework = request.POST.getlist("homework")
- for a,item in enumerate(ClassStudyRecord.objects.filter(pk=id).first().class_obj.student_set.all()):
- print(a,item)
- StudentStudyRecord.objects.filter(student=item).update(score=user[a],homework_note=homework[a])
- return redirect(request.path)
views.py录入成绩
- <table>
- <form action="" method="post">
- {% csrf_token %}
- <thead>
- <tr>
- <th>编号</th>
- <th>姓名</th>
- <th>考勤</th>
- <th>成绩</th>
- <th>批语</th>
- </tr>
- </thead>
- <tbody>
- {% for consult_record in ret %}
- <tr>
- <td>{{ forloop.counter }}</td>
- <td>{{ consult_record.student }}</td>
- <td>{{ consult_record.get_record_display }}</td>
- <td><select name="user" id="">
- {% for foo in consult_record.score_choices %}
- #进行判断,若是存在值添加selected让其显示,没有显示默认
- {% if consult_record.score == foo.0 %}
- <option selected value="{{ foo.0 }}">{{ foo.1 }}</option>
- {% else %}
- <option value="{{ foo.0 }}">{{ foo.1 }}</option>
- {% endif %}
- {% endfor %}
- <option value=""></option>
- </select>
- </td>
- <td><input type="text" name="homework" value="{{ consult_record.homework_note }}"></td>
- </tr>
- {% endfor %}
- </tbody>
- <button class="btn btn-default">提交</button>
- </form>
- </table>
前端.html
modelformset 简单版
1.首先引入模块
- from django.forms.models import modelformset_factory
2.
后端
- from django.forms.models import modelformset_factory
- class StudentStudyRecordModelForm(forms.ModelForm):
- class Meta:
- model=StudentStudyRecord
- fields=["score","homework_note"]
- class RecordScoreView(View):
- def get(self, request,class_study_record_id):
- #modelformset_factory()里面需要2个变量,model和modelform,extra默认为1,显示添加
- model_formset_cls=modelformset_factory(model=StudentStudyRecord,form=StudentStudyRecordModelForm,extra=0)
- queryset = StudentStudyRecord.objects.filter(classstudyrecord=class_study_record_id)
- formset = model_formset_cls(queryset=queryset)
- return render(request,"student/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)
- if formset.is_valid():
- formset.save()
- print(formset.errors)
- return redirect(request.path)
modelformset 批量编辑
3.
前端
- <hr>
- <div class="panel panel-default">
- <div class="panel-heading">学习记录</div>
- <div class="panel-body">
- <div style="width: 680px;margin: 0 auto;">
- <form method="post" action="">
- {% csrf_token %}
- #这个必须要写
- {{ formset.management_form }}
- <table class="table table-bordered">
- <thead>
- <tr>
- <th>姓名</th>
- <th>考勤</th>
- <th>作业成绩</th>
- <th>作业评语</th>
- </tr>
- </thead>
- <tbody>
- {% for form in formset %}
- <tr>
- {{ form.id }}
- #form.instance.为显示界面,不添加编辑页面
- <td>{{ form.instance.student }}</td>
- <td>{{ form.instance.get_record_display }} </td>
- <td>{{ form.score }} </td>
- <td>{{ form.homework_note }}</td>
- </tr>
- {% endfor %}
- </tbody>
- </table>
- <input type="submit" value="保存">
- </form>
- </div>
- </div>
- </div>
- <hr>
前端.html
django modelformse批量编辑 查询学生班级成绩的更多相关文章
- SQL 查询:查询学生平均成绩
编程萌新,因为遇到这么个SQL 查询的问题:在一张表A里有如下字段:学生姓名.学科名.学科成绩.写一条SQL 语句查出各科平均成绩并按学生姓名分组,按如下格式显示:学生姓名|语文|数学|英语.一开始遇 ...
- mysql 查询每个班级成绩前两名
- sql-hive笔试题整理 1 (学生表-成绩表-课程表-教师表)
题记:一直在写各种sql查询语句,最长的有一百多行,自信什么需求都可以接,可......,想了想,可能一直在固定的场景下写,平时也是以满足实际需求为目的,竟不知道应试的题都是怎么出的,又应该怎么做.遂 ...
- SQL使用子查询,查找班级成绩最高分
-- 根据要求,获取班级成绩的最高分的学生-- 第一个子查询,先去各个科目的最高,再横向比较各个科目的最高,再取最高分的那个科目-- 第二个子查询,查询每个同学的最高分-- 最后,通过第一个子查询查询 ...
- 《MySQL数据操作与查询》- 维护学生信息、老师信息和成绩信息 支持按多种条件组合查询学生信息和成绩信息
综合项目需求 一.系统整体功能 系统需支持以下功能: 维护学生信息.老师信息和成绩信息 支持按多种条件组合查询学生信息和成绩信息 学生 Student(id,班级id,学号,姓名,性别,电话,地址,出 ...
- Django ORM多表查询练习
ORM多表查询 创建表结构: from django.db import models # 创建表结构 # Create your models here. class Class_grade(mod ...
- Django聚合与分组查询中value与annotate的顺序问题
在学习Django聚合与分组查询中,发现value与annotate的顺序不同时,查询结果大相径庭,经过一下午的研究,终于弄明白了,现在分享给大家,先上结论: 结论 value在annotate前面时 ...
- Django的ORM常用查询操作总结(Django编程-3)
Django的ORM常用查询操作总结(Django编程-3) 示例:一个Student model: class Student(models.Model): name=models.CharFiel ...
- Django框架 之 ORM查询操作详解
Django框架 之 ORM查询操作详解 浏览目录 一般操作 ForeignKey操作 ManyToManyField 聚合查询 分组查询 F查询和Q查询 事务 Django终端打印SQL语句 在Py ...
随机推荐
- [LeetCode] 146. LRU Cache 最近最少使用页面置换缓存器
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...
- css设置不可复制
-moz-user-select:none; /* Firefox私有属性 */ -webkit-user-select:none; /* WebKit内核私有属性 */ -ms-user-selec ...
- Python安装(64位Win8.1专业版)
本文出处:http://www.cnblogs.com/leonwen/p/4700648.html 嗯,开始学Python. 我安装的是Python 2.7.10版本,安装的时候除了选了路径其他均n ...
- java语言规范
一.标志符 命名规则: 标识符由26个英文字符大小写(a~zA~Z).数字(0~9).下划线(_)和美元符号($)组成. 不能以数字开头,不能是关键字 严格区分大小写 标识符的可以为任意长度 命名规范 ...
- http与tcp,udp的区别
1.网络协议的概念 (1)在学习网络课程的时候,老师会讲iso七层模型,有应用层 表示层 会话层 传输层 网络层 数据链路层 物理层,其中http就属于应用层,tcp与udp是属于传输层,如图1.1( ...
- LayUI-Table-添加禁止选中
LayUI这几年比较流行,里面的Table组件也比较强大,但是前面的CheckBox没有禁止选中功能,今天就来试试,看看能不能给添加一个禁止选中功能. Fork LayUI源码 LayUI项目地址 C ...
- Python Web编程
1.统一资源定位符(URL) URL用来在Web上定位一个文档.浏览器只是Web客户端的一种,任何一个向服务器端发送请求来获取数据的应用程序都被认为是客户端 URL格式:port_sch://net_ ...
- php 500报错解决方案
php 500报错解决方案 1 先看nginx error.log 指定的错误日记文件路径 找到这个日记文件看 里面信息 2 再看 php-fpm.conf 里面指定的PHP错误日记的路径 具体如下& ...
- lua中,两种json和table互转方法的效率比较
lua中json和table的互转,是我们在平时开发过程中经常用到的.比如: 在用lua编写的服务器中,如果客户端发送json格式的数据,那么在lua处理业务逻辑的时候,必然需要转换成lua自己的数据 ...
- Linq 将两个查询结果合称为一个
var handsonitems = from a in db.DltQuestionHandson join c in db.DltBdChapter on new { a.ChapterCode ...