复习先知

  1. 关于三张表的编辑学生成绩
    在跨表查询的对象查询种,只能通过找到两张表的关联的对象,
    进行跨表,就是在一对多或多对多的模型找到他们俩的class_idstudent_id
    在关联时,会通过他们找到所要查询的id,或字段,进而找到他们的字段
    进而查询出对象,这个表的对象, .出来
    在这种跨三表的途中,编辑跟添加同理.都是通过过
    过滤出你要找到学生的对象 学生成绩关联班级成绩 班级成绩关联班级 班级关联学生
    这就是这张表的精髓
  2.  
  3. 关于modelformset 可以省去复杂跨表的骚操作
    也可以一次性多删除
    第一步
    引入from django.forms.models import modelformset_factory
  4.  
  5. modelformset_factory(含2个变量,还有一个默认为1)
    model() modelform() extra=1
    1.实例化对象
    2.找到点击的对象 queryset对象
    3.实例化对象的方法(queryset=queryset)
    返回给页面 自动给你
    页面: 一样 但需要标注{{formset.management_form}}
  6.  
  7. 在编辑时候,
    1.实例化对象modelformset_factory()
    2.formset(request.post)
    3.if formset.is_valid() form的通病
    4.formset.save()

复杂版

将字段设置联合唯一

  1. 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. def post(self, request):
  7. print(request.POST)
  8. func_st = request.POST.get("action")
  9. num = request.POST.getlist("selected_pk_list")
  10.  
  11. print("num", num)
  12. if not hasattr(self, func_st):
  13. return HttpResponse("非法输入")
  14. else:
  15. func = getattr(self, func_st)
  16. queryset = ClassStudyRecord.objects.filter(pk__in=num)
  17.  
  18. print("queryset", queryset)
  19. ret = func(request, queryset)
  20. if ret:
  21. return ret
  22. return redirect(request.path)
  23. #批量删除
  24. def patch_delete(self, request, queryset):
  25. queryset.delete()
  26. #批量查询,利用跨表查询查询创建字段,为了避免添加重复使用联合唯一
  27. def patch_choice(self,request,queryset):
  28. try:
  29. for i in queryset:
  30. for item in i.class_obj.student_set.all():
  31. StudentStudyRecord.objects.create(student=item,classstudyrecord=i)
  32. except Exception as e:
  33. 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. </tbody>
  39. <button class="btn btn-default">提交</button>
  40.  
  41. </form>
  42. </table>

前端.html

modelformset 简单版

1.首先引入模块

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

前端.html

  1.  

django modelformse批量编辑 查询学生班级成绩的更多相关文章

  1. SQL 查询:查询学生平均成绩

    编程萌新,因为遇到这么个SQL 查询的问题:在一张表A里有如下字段:学生姓名.学科名.学科成绩.写一条SQL 语句查出各科平均成绩并按学生姓名分组,按如下格式显示:学生姓名|语文|数学|英语.一开始遇 ...

  2. mysql 查询每个班级成绩前两名

  3. sql-hive笔试题整理 1 (学生表-成绩表-课程表-教师表)

    题记:一直在写各种sql查询语句,最长的有一百多行,自信什么需求都可以接,可......,想了想,可能一直在固定的场景下写,平时也是以满足实际需求为目的,竟不知道应试的题都是怎么出的,又应该怎么做.遂 ...

  4. SQL使用子查询,查找班级成绩最高分

    -- 根据要求,获取班级成绩的最高分的学生-- 第一个子查询,先去各个科目的最高,再横向比较各个科目的最高,再取最高分的那个科目-- 第二个子查询,查询每个同学的最高分-- 最后,通过第一个子查询查询 ...

  5. 《MySQL数据操作与查询》- 维护学生信息、老师信息和成绩信息 支持按多种条件组合查询学生信息和成绩信息

    综合项目需求 一.系统整体功能 系统需支持以下功能: 维护学生信息.老师信息和成绩信息 支持按多种条件组合查询学生信息和成绩信息 学生 Student(id,班级id,学号,姓名,性别,电话,地址,出 ...

  6. Django ORM多表查询练习

    ORM多表查询 创建表结构: from django.db import models # 创建表结构 # Create your models here. class Class_grade(mod ...

  7. Django聚合与分组查询中value与annotate的顺序问题

    在学习Django聚合与分组查询中,发现value与annotate的顺序不同时,查询结果大相径庭,经过一下午的研究,终于弄明白了,现在分享给大家,先上结论: 结论 value在annotate前面时 ...

  8. Django的ORM常用查询操作总结(Django编程-3)

    Django的ORM常用查询操作总结(Django编程-3) 示例:一个Student model: class Student(models.Model): name=models.CharFiel ...

  9. Django框架 之 ORM查询操作详解

    Django框架 之 ORM查询操作详解 浏览目录 一般操作 ForeignKey操作 ManyToManyField 聚合查询 分组查询 F查询和Q查询 事务 Django终端打印SQL语句 在Py ...

随机推荐

  1. [LeetCode] 146. LRU Cache 最近最少使用页面置换缓存器

    Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...

  2. css设置不可复制

    -moz-user-select:none; /* Firefox私有属性 */ -webkit-user-select:none; /* WebKit内核私有属性 */ -ms-user-selec ...

  3. Python安装(64位Win8.1专业版)

    本文出处:http://www.cnblogs.com/leonwen/p/4700648.html 嗯,开始学Python. 我安装的是Python 2.7.10版本,安装的时候除了选了路径其他均n ...

  4. java语言规范

    一.标志符 命名规则: 标识符由26个英文字符大小写(a~zA~Z).数字(0~9).下划线(_)和美元符号($)组成. 不能以数字开头,不能是关键字 严格区分大小写 标识符的可以为任意长度 命名规范 ...

  5. http与tcp,udp的区别

    1.网络协议的概念 (1)在学习网络课程的时候,老师会讲iso七层模型,有应用层 表示层 会话层 传输层 网络层 数据链路层 物理层,其中http就属于应用层,tcp与udp是属于传输层,如图1.1( ...

  6. LayUI-Table-添加禁止选中

    LayUI这几年比较流行,里面的Table组件也比较强大,但是前面的CheckBox没有禁止选中功能,今天就来试试,看看能不能给添加一个禁止选中功能. Fork LayUI源码 LayUI项目地址 C ...

  7. Python Web编程

    1.统一资源定位符(URL) URL用来在Web上定位一个文档.浏览器只是Web客户端的一种,任何一个向服务器端发送请求来获取数据的应用程序都被认为是客户端 URL格式:port_sch://net_ ...

  8. php 500报错解决方案

    php 500报错解决方案 1 先看nginx error.log 指定的错误日记文件路径 找到这个日记文件看 里面信息 2 再看 php-fpm.conf 里面指定的PHP错误日记的路径 具体如下& ...

  9. lua中,两种json和table互转方法的效率比较

    lua中json和table的互转,是我们在平时开发过程中经常用到的.比如: 在用lua编写的服务器中,如果客户端发送json格式的数据,那么在lua处理业务逻辑的时候,必然需要转换成lua自己的数据 ...

  10. Linq 将两个查询结果合称为一个

    var handsonitems = from a in db.DltQuestionHandson join c in db.DltBdChapter on new { a.ChapterCode ...