[英]Django getlist() bug?
我没有收到任何错误,但我知道数据库中的保存记录不正确,是 getlist() 中的错误吗? 还是我错过了代码中的某些内容? 请告诉我为什么错误的记录保存在数据库中
def corevalues(request):
coregradelevel = request.GET.get('coregradelevel')
coreperiod = request.GET.get('coreperiod')
marking = StudentBehaviorMarking.objects.all()
teacher = request.GET.get('teacher')
corevalues = CoreValues.objects.all().order_by('Display_Sequence')
corevaluesdescription = CoreValuesDescription.objects.values('id','Description').distinct('Description').order_by('Description')
corevaluesperiod = CoreValuesDescription.objects.filter(grading_Period=coreperiod).order_by('Display_Sequence')
period = gradingPeriod.objects.filter(id=coreperiod).order_by('Display_Sequence')
gradelevel = EducationLevel.objects.filter(id__in = coregradelevel).distinct().order_by('id')
studentcorevalues = StudentsCoreValuesDescription.objects.filter(Teacher = teacher).filter(GradeLevel = gradelevel.values_list('Description'))\
.values('Students_Enrollment_Records').distinct('Students_Enrollment_Records').order_by('Students_Enrollment_Records')
student = StudentPeriodSummary.objects.filter(Teacher = teacher).filter(GradeLevel__in = gradelevel.values_list('id'))
这是我的 html 代码
<table class="tblcore">
<tr>
<td rowspan="2" colspan="2">Core Values</td>
{% for core in corevalues %}
<td colspan="8"><input type="hidden" value="{{core.id}}" name="core">{{core.Description}}</td>
{% endfor %}
</tr>
<tr>
{% for corevalues in corevaluesperiod %}
<td colspan="4" style="font-size: 12px"><input type="hidden" value="{{corevalues.id}}" name="coredescription">{{corevalues.Description}}</td>
{% endfor %}
</tr>
<tr>
<td colspan="2">Student's Name</td>
{% for corevalues in period %}
<td colspan="4">
<input type="hidden" value="{{corevalues.id}}" name="coreperiod">Q {{corevalues.id}}
</td>
<td colspan="4">
<input type="hidden" value="{{corevalues.id}}" name="coreperiod">Q {{corevalues.id}}
</td>
<td colspan="4">
<input type="hidden" value="{{corevalues.id}}" name="coreperiod">Q {{corevalues.id}}
</td>
<td colspan="4">
<input type="hidden" value="{{corevalues.id}}" name="coreperiod">Q {{corevalues.id}}
</td>
<td colspan="4">
<input type="hidden" value="{{corevalues.id}}" name="coreperiod">Q {{corevalues.id}}
</td>
<td colspan="4">
<input type="hidden" value="{{corevalues.id}}" name="coreperiod">Q {{corevalues.id}}
</td>
<td colspan="4">
<input type="hidden" value="{{corevalues.id}}" name="coreperiod">Q {{corevalues.id}}
</td>
{% endfor %}
</tr>
{% for students in student %}
<tr>
<td colspan="2"><input type="hidden" value="{{students.id}}" name="student">{{students.Students_Enrollment_Records.Students_Enrollment_Records.Students_Enrollment_Records.Student_Users}}</td>
<td colspan="4">
<select name="marking">
<option>--------</option>
{% for m in marking %}
<option value="{{m.Marking}}" name="marking">{{m.Marking}}</option>
{% endfor %}
</select>
</td>
<td colspan="4">
<select>
<option>--------</option>
{% for m in marking %}
<option value="{{m.id}}">{{m.Marking}}</option>
{% endfor %}
</select>
</td>
<td colspan="4">
<select>
<option>--------</option>
{% for m in marking %}
<option value="{{m.id}}">{{m.Marking}}</option>
{% endfor %}
</select>
</td>
<td colspan="4">
<select>
<option>--------</option>
{% for m in marking %}
<option value="{{m.id}}">{{m.Marking}}</option>
{% endfor %}
</select>
</td>
<td colspan="4">
<select>
<option>--------</option>
{% for m in marking %}
<option value="{{m.id}}">{{m.Marking}}</option>
{% endfor %}
</select>
</td>
<td colspan="4">
<select>
<option>--------</option>
{% for m in marking %}
<option value="{{m.id}}">{{m.Marking}}</option>
{% endfor %}
</select>
</td>
<td colspan="4">
<select>
<option>--------</option>
{% for m in marking %}
<option value="m.id">{{m.Marking}}</option>
{% endfor %}
</select>
</td>
</tr>
{% endfor %}
</table>
这是它的样子
这是我插入数据的代码
global marking,coreperiod,corevalues
id = request.POST.get('teacher')
teacher = EmployeeUser(id=id)
coreid = request.POST.get('core')
core = CoreValues(id=coreid)
corevalues = []
for corevaluesid in request.POST.getlist('coredescription'):
corevalues.append(corevaluesid)
coreperiodID = request.POST.get('coreperiod')
coreperiod = gradingPeriod(id = coreperiodID)
marking = []
for markingID in request.POST.getlist('marking'):
marking.append(markingID)
print(markingID)
for desc,m in enumerate(request.POST.getlist('marking')):
coredesc = corevalues[desc]
print(corevalues[desc])
coredescription = CoreValuesDescription(id=coredesc)
print("behavior",m)
for student in request.POST.getlist('student'):
students = StudentPeriodSummary(id=student)
V_insert_data = StudentsCoreValuesDescription(
Core_Values=coredescription,
Marking=m,
Students_Enrollment_Records=students,
grading_Period = coreperiod,
)
V_insert_data.save()
您的某些选择选项没有名称,这就是它没有获取所有数据的原因:
<form method="post" action="/studentbehavior/" id="myform" class="myform" style="width: 100%" enctype="multipart/form-data">{% csrf_token %}
<table class="tblcore">
<tr>
<td rowspan="2" colspan="2">Core Values</td>
{% for core in corevalues %}
<td colspan="8"><input type="hidden" value="{{core.id}}" name="core">{{core.Description}}</td>
{% endfor %}
</tr>
<tr>
{% for corevalues in corevaluesperiod %}
<td colspan="4" style="font-size: 12px"><input type="hidden" value="{{corevalues.id}}" name="coredescription">{{corevalues.Description}}</td>
{% endfor %}
</tr>
<tr>
<td colspan="2">Student's Name</td>
{% for corevalues in period %}
<td colspan="4">
<input type="hidden" value="{{corevalues.id}}" name="coreperiod">Q {{corevalues.id}}
</td>
<td colspan="4">
<input type="hidden" value="{{corevalues.id}}" name="coreperiod">Q {{corevalues.id}}
</td>
<td colspan="4">
<input type="hidden" value="{{corevalues.id}}" name="coreperiod">Q {{corevalues.id}}
</td>
<td colspan="4">
<input type="hidden" value="{{corevalues.id}}" name="coreperiod">Q {{corevalues.id}}
</td>
<td colspan="4">
<input type="hidden" value="{{corevalues.id}}" name="coreperiod">Q {{corevalues.id}}
</td>
<td colspan="4">
<input type="hidden" value="{{corevalues.id}}" name="coreperiod">Q {{corevalues.id}}
</td>
<td colspan="4">
<input type="hidden" value="{{corevalues.id}}" name="coreperiod">Q {{corevalues.id}}
</td>
{% endfor %}
</tr>
{% for students in student %}
<tr>
<td colspan="2"><input type="hidden" value="{{students.id}}" name="student">{{students.Students_Enrollment_Records.Students_Enrollment_Records.Students_Enrollment_Records.Student_Users}}</td>
<td colspan="4">
<select name="marking">
<option>--------</option>
{% for m in marking %}
<option value="{{m.Marking}}" >{{m.Marking}}</option>
{% endfor %}
</select>
</td>
<td colspan="4">
<select name="marking">
<option >--------</option>
{% for m in marking %}
<option value="{{m.Marking}}">{{m.Marking}}</option>
{% endfor %}
</select>
</td>
<td colspan="4">
<select name="marking">
<option>--------</option>
{% for m in marking %}
<option value="{{m.Marking}}">{{m.Marking}}</option>
{% endfor %}
</select>
</td>
<td colspan="4">
<select name="marking">
<option>--------</option>
{% for m in marking %}
<option value="{{m.Marking}}">{{m.Marking}}</option>
{% endfor %}
</select>
</td>
<td colspan="4">
<select name="marking">
<option>--------</option>
{% for m in marking %}
<option value="{{m.Marking}}">{{m.Marking}}</option>
{% endfor %}
</select>
</td>
<td colspan="4">
<select name="marking">
<option>--------</option>
{% for m in marking %}
<option value="{{m.Marking}}">{{m.Marking}}</option>
{% endfor %}
</select>
</td>
<td colspan="4">
<select name="marking">
<option>--------</option>
{% for m in marking %}
<option value="{{m.Marking}}">{{m.Marking}}</option>
{% endfor %}
</select>
</td>
</tr>
{% endfor %}
</table>
<input type="submit" value="Update">
</form>
并对我的观点这样做
for desc, m in enumerate(request.POST.getlist('marking')):
coredesc = corevalues[desc]
coredescription = CoreValuesDescription(id=coredesc)
for student in request.POST.getlist('student'):
students = StudentPeriodSummary(id=student)
V_insert_data = StudentsCoreValuesDescription(
#Teacher=teacher,
Core_Values=coredescription,
Marking=m,
Students_Enrollment_Records=students,
grading_Period = coreperiod,
)
V_insert_data.save()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.