Django使用getlist更新数据 [英] Django Update data using getlist
本文介绍了Django使用getlist更新数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我如何更新每个标记为学生的学生?
How do i update every students marking?
这是我的管理站点
this is my admin site
我区分学生姓名
这是我的观点。py
//I distinct duplicate student name
student = StudentsBehaviorGrades.objects.filter(Teacher = teacher).filter(Grading_Period = coreperiod).values\
('Students_Enrollment_Records__Students_Enrollment_Records__Students_Enrollment_Records__Students_Enrollment_Records__Student_Users__Lastname','Students_Enrollment_Records__Students_Enrollment_Records__Students_Enrollment_Records__Students_Enrollment_Records__Student_Users__Firstname','id')\
.distinct('Students_Enrollment_Records__Students_Enrollment_Records__Students_Enrollment_Records__Students_Enrollment_Records__Student_Users__Firstname','Students_Enrollment_Records__Students_Enrollment_Records__Students_Enrollment_Records__Students_Enrollment_Records__Student_Users__Lastname')\
.order_by('Students_Enrollment_Records__Students_Enrollment_Records__Students_Enrollment_Records__Students_Enrollment_Records__Student_Users__Firstname','Students_Enrollment_Records__Students_Enrollment_Records__Students_Enrollment_Records__Students_Enrollment_Records__Student_Users__Lastname')
//I display the marking of students mark (selection box) this is correct
studentsbehaviorgrade = StudentsBehaviorGrades.objects.filter(Teacher=teacher).filter(Grading_Period=coreperiod).order_by('Grading_Behavior__Display_Sequence')
这是我的html
{% for students in student %}
<tr>
<td colspan="2"><input type="hidden" value="{{students.id}}" name="id">{{students.Students_Enrollment_Records__Students_Enrollment_Records__Students_Enrollment_Records__Students_Enrollment_Records__Student_Users__Lastname}} {{students.Students_Enrollment_Records__Students_Enrollment_Records__Students_Enrollment_Records__Students_Enrollment_Records__Student_Users__Firstname}}</td>
{% for be in studentsbehaviorgrade %}
<td colspan="4">
<select name="Marking">
<option value="{{be.Marking.id}}">{{be.Marking}}</option>
{% for behaviors in behavior %}
<option value="{{behaviors.id}}">{{behaviors.Marking}}</option>
{% endfor %}
</select>
</td>
{% endfor %}
</tr>
{% endfor %}
这就是结果
这是我进行更新的逻辑。
this is my logic on update.
marks=[]
for mark in request.POST.getlist('Marking'):
marks.append(mark)
print(marks)
for s, student in enumerate(request.POST.getlist('Marking')):
studentgrade = marks[s]
print(studentgrade)
psa = StudentBehaviorMarking(id=studentgrade)
update = StudentsBehaviorGrades.objects.get(Marking = student)
update.Marking=psa
update.save()
return render(request, 'Homepage/pending.html')
推荐答案
尝试
{% for students in student %}
<tr>
<td colspan="2"><input type="hidden" value="{{students.id}}" >{{students.Students_Enrollment_Records__Students_Enrollment_Records__Students_Enrollment_Records__Students_Enrollment_Records__Student_Users__Lastname}} {{students.Students_Enrollment_Records__Students_Enrollment_Records__Students_Enrollment_Records__Students_Enrollment_Records__Student_Users__Firstname}}</td>
{% for corevalues in studentsbehaviorgrade %}
<td colspan="4">
<input type="hidden" value="{{corevalues.id}}" name="id">
<select name="Marking">
<option value="{{corevalues.Marking.id}}" >{{corevalues.Marking.id}}-{{corevalues.Marking.Marking}}</option>
{% for behaviors in behavior %}
<option value="{{behaviors.id}}">{{behaviors.id}}-{{behaviors.Marking}}</option>
{% endfor %}
</select>
</td>
{% endfor %}
</tr>
{% endfor %}
并在更新时将其复制到您的逻辑中
and copy this in your logic in update
marks=[]
for mark in request.POST.getlist('Marking'):
marks.append(mark)
#print(marks)
corevalues = []
for corevaluesid in request.POST.getlist('coredescription'):
corevalues.append(corevaluesid)
for i, student in enumerate(request.POST.getlist('id')):
marked =marks[i]
psa = StudentBehaviorMarking(id=marked)
update = StudentsBehaviorGrades.objects.get(id=student)
update.Marking = psa
update.save()
这篇关于Django使用getlist更新数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文