码迷,mamicode.com
首页 > 其他好文 > 详细

Django 使用 modelformset 组件批量修改表单数据

时间:2020-01-18 13:06:25      阅读:106      评论:0      收藏:0      [点我收藏+]

标签:student   value   style   panel   ken   blank   submit   使用   实例   

1.models.py:
class StudentStudyRecord(models.Model):
‘‘‘
学生学习记录
‘‘‘
student = models.ForeignKey(verbose_name="学员", to=‘Student‘,on_delete=models.CASCADE)
classstudyrecord = models.ForeignKey(verbose_name="第几天课程", to="ClassStudyRecord",on_delete=models.CASCADE)
record_choices = ((‘checked‘, "已签到"),
(‘vacate‘, "请假"),
(‘late‘, "迟到"),
(‘noshow‘, "缺勤"),
(‘leave_early‘, "早退"),
)
record = models.CharField("上课纪录", choices=record_choices, default="checked", max_length=64)
score_choices = ((100, ‘A+‘),
(90, ‘A‘),
(85, ‘B+‘),
(80, ‘B‘),
(70, ‘B-‘),
(60, ‘C+‘),
(50, ‘C‘),
(40, ‘C-‘),
(0, ‘ D‘),
(-1, ‘N/A‘),
(-100, ‘COPY‘),
(-1000, ‘FAIL‘),
)
score = models.IntegerField("本节成绩", choices=score_choices, default=-1)
homework_note = models.CharField(verbose_name=‘作业评语‘, max_length=255, blank=True, null=True)
note = models.CharField(verbose_name="备注", max_length=255, blank=True, null=True)

homework = models.FileField(verbose_name=‘作业文件‘, blank=True, null=True, default=None)
stu_memo = models.TextField(verbose_name=‘学员备注‘, blank=True, null=True)
date = models.DateTimeField(verbose_name=‘提交作业日期‘, auto_now_add=True)

def __str__(self):
return "{0}-{1}".format(self.classstudyrecord, self.student)

class Meta:
unique_together=["student","classstudyrecord"]

2.views.py
from django.forms import modelform
#对学生记录表建立一个modelform
class StudentStudyRecordForm(forms.ModelForm):

class Meta:
model = models.StudentStudyRecord
fields = ["score","homework_note"] #要在前端修改的字段


#批量修改的学生成绩视图类

class RecordScoreView(View):

def get(self, request, csr_id):

from django.forms.models import modelformset_factory#导入组件!

model_formset_cls = modelformset_factory(model=models.StudentStudyRecord,form=StudentStudyRecordForm,extra=0)#把数据库表实例化一个对象

queryset = models.StudentStudyRecord.objects.filter(classstudyrecord=csr_id)

formset = model_formset_cls(queryset=queryset)

return render(request, ‘student/record_score.html‘, locals())

def post(self, request, csr_id):
from django.forms.models import modelformset_factory
model_formset_cls = modelformset_factory(model=models.StudentStudyRecord,form=StudentStudyRecordForm,extra=0)
queryset = models.StudentStudyRecord.objects.filter(classstudyrecord=id)
formset = model_formset_cls(request.POST)#提交post请求,校验成功直接保存.
if formset.is_valid():
formset.save()
return self.get(request, csr_id=csr_id)


3.html:
    <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 }}  #后端使用modelformset组件时,前端必须加上这一句话!

<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 }}
<td>{{ form.instance.student }}</td>   #instance加上,前端渲染的页面就没有这个修改项
<td>{{ form.instance.get_record_display }} </td>
<td>{{ form.score }} </td>
<td>{{ form.homework_note }}</td>
</tr>
{% endfor %}
</tbody>
</table>
<input type="submit" value="保存" class="btn-danger">
</form>
</div>
</div>



</div>
</div>


Django 使用 modelformset 组件批量修改表单数据

标签:student   value   style   panel   ken   blank   submit   使用   实例   

原文地址:https://www.cnblogs.com/cou1d/p/12208429.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!