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

Posted cou1d

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Django 使用 modelformset 组件批量修改表单数据相关的知识,希望对你有一定的参考价值。

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 组件批量修改表单数据的主要内容,如果未能解决你的问题,请参考以下文章

在基于 Django 类的视图中使用 modelformset_factory

Django:如何使用 ModelFormSet 和 FormWizard 过滤 ForeignKey 选择(例如使用 request.user)?

元类与 Django 中的 modelformset_factory 冲突

django modelformset 排除 ID 字段

解析json在模板中的django modelformset中返回

Django modelformset order_by 不工作