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 ...
随机推荐
- ASP.NET开发实战——(三)第一个ASP.NET应用《MyBlog》
本文开始通过ASP.NET MVC创建一个博客应用,该应用是通过默认的MVC模板修改而来,所以创建的过程和代码都与默认模板一致,然后通过修改的方式将默认模板改为博客的主页,并添加博客列表.内容等页面. ...
- 洛谷 P1950 长方形_NOI导刊2009提高(2)
传送门 思路 首先定义\(h\)数组,\(h[i][j]\)表示第\(i\)行第\(j\)列最多可以向上延伸多长(直到一个被用过的格子) 然后使用单调栈算出 \(l_i\)和 \(r_i\) ,分别是 ...
- Linux性能优化实战学习笔记:第三十七讲
一.上节回顾 上一节,我带你一起学习了网络性能的评估方法.简单回顾一下,Linux 网络基于 TCP/IP协议栈构建,而在协议栈的不同层,我们所关注的网络性能也不尽相同. 在应用层,我们关注的是应用程 ...
- [LeetCode] 230. Kth Smallest Element in a BST 二叉搜索树中的第K小的元素
Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Not ...
- Word文字中嵌套的图片向上突出,与文字的高度不一致
文字中嵌套的图片向上突出,与文字的高度不一致. 调整方法: 选中图片,找到Font字体设置,选中位置下拉框,选择适当项. Select the inline graphic by clicking o ...
- ASP.NET的MVC请求处理流程
1.用户打开浏览器,在地址栏输入某个网址的URL并回车,浏览器便开始像该URL指定的服务器发起HTTP请求 .2.服务器的网站服务系统(IIS)接收到该请求,先检查自己是否认识该类请求,如果认识就直接 ...
- Laravel自定义排序
如果数据库的status字段有0,1,2,3几种状态,如果想让status为1,2的状态排在最前面 那么可以这样: $obj = $obj->orderByRaw(DB::raw('FIELD( ...
- vue需求表单有单位(时分秒千克等等)
需求如下: 问题分析: 因为用elementui组件 el-input 相当于块级元素,后面的单位<span>分</span>会被挤下去,无法在同一水平. 解决方法: 不用它的 ...
- linux内核debug的一种方式:procfs
#include <linux/module.h> #include <linux/compat.h> #include <linux/types.h> #incl ...
- arcgis js api 4.X 自定义工具按钮
// All material copyright ESRI, All Rights Reserved, unless otherwise specified. // See https://js.a ...