Python 如何插入每个学生的正确值,为每个核心值选择每个分数
我有一个相关的问题。我只是不知道如何获得正确的数据 就像这张照片一样 (对于每个学生,为每个corevalues选择每个分数(是展示自豪感…)): 这是my views.py:Python 如何插入每个学生的正确值,为每个核心值选择每个分数,python,django,django-views,Python,Django,Django Views,我有一个相关的问题。我只是不知道如何获得正确的数据 就像这张照片一样 (对于每个学生,为每个corevalues选择每个分数(是展示自豪感…)): 这是my views.py: for i, mark in enumerate(request.POST.getlist('Marking')): p = marking[int(mark)] s = StudentBehaviorMarking(id=p) core = corevalues[i] cores =
for i, mark in enumerate(request.POST.getlist('Marking')):
p = marking[int(mark)]
s = StudentBehaviorMarking(id=p)
core = corevalues[i]
cores = EducationLevelGradingBehavior(id=core)
print(s, cores)
for student in request.POST.getlist('student'):
students = StudentPeriodSummary(id=student)
V_insert_data = StudentsBehaviorGrades(
Teacher=teacher,
#Education_Levels
Students_Enrollment_Records = students,
Grading_Period = coreperiod,
Grading_Behavior = cores,
Marking = s,
)
V_insert_data.save()
和我的HTML:
{BehaviorPerLevel%中的corevalues为%0}
{{corevalues.Grading_Behavior.GroupName}
{%endfor%}
学名
{BehaviorPerLevel%中的corevalues为%0}
Q{{corevalues.Grading_Period.id}
{%endfor%}
{学生百分比中的学生百分比}
{{学生.学生注册记录.学生注册记录.学生注册记录.学生注册记录.学生用户}
{BehaviorPerLevel%中的corevalues为%0}
{%用于行为%]中的行为
{{行为.标记}
{%endfor%}
{%endfor%}
{%endfor%}
这是my models.py:
class GradingBehavior(models.Model):
Display_Sequence = models.IntegerField(null=True, blank=True)
Name = models.CharField(max_length=500, null=True, blank=True)
Description = models.CharField(max_length=500, null=True, blank=True)
GroupName = models.CharField(max_length=500, null=True, blank=True)
GroupDescription = models.CharField(max_length=500, null=True, blank=True)
//this is models corevalues
class EducationLevelGradingBehavior(models.Model):
Display_Sequence = models.IntegerField(null=True, blank=True)
GradeLevel = models.ForeignKey(EducationLevel, on_delete=models.CASCADE,
null=True, blank=True)
Grading_Behavior = models.ForeignKey(GradingBehavior, on_delete=models.CASCADE, null=True, blank=True)
Grading_Period = models.ForeignKey(gradingPeriod, on_delete=models.CASCADE, null=True, blank=True)
//this is the models where the user selecting the marks for every core values
class StudentBehaviorMarking(models.Model):
Marking = models.CharField(max_length=500, null=True, blank=True)
Non_numerical_Rating = models.CharField(max_length=500, null=True, blank=True)
//this is the model where the data saves
class StudentsBehaviorGrades(models.Model):
Teacher = models.ForeignKey(EmployeeUser, on_delete=models.CASCADE,
null=True, blank=True)
Education_Levels = models.ForeignKey(EducationLevel, on_delete=models.CASCADE, null=True, blank=True)
Students_Enrollment_Records = models.ForeignKey(StudentPeriodSummary,
on_delete=models.CASCADE, null=True)
Grading_Period = models.ForeignKey(gradingPeriod, on_delete=models.CASCADE, null=True, blank=True)
Grading_Behavior = models.ForeignKey(EducationLevelGradingBehavior, on_delete=models.CASCADE, null=True, blank=True)
Marking = models.ForeignKey(StudentBehaviorMarking,
on_delete=models.CASCADE, null=True)
当我尝试插入数据时
结果是:
如您所见,我插入的数据没有正确匹配,我没有为每个核心值获得每个学生分数的正确值 在您的视图中尝试此选项。py
marking = []
for markingID in request.POST.getlist('Marking'):
marking.append(markingID)
for i, mark in enumerate(request.POST.getlist('Marking')):
s = StudentBehaviorMarking(id=mark)
core = corevalues[i]
cores = EducationLevelGradingBehavior(id=core)
print("mark", mark, "per description", core)
for student in request.POST.getlist('student'):
students = StudentPeriodSummary(id=student)
V_insert_data = StudentsBehaviorGrades(
Teacher=teacher,
Students_Enrollment_Records=students,
Grading_Period=coreperiod,
Grading_Behavior=cores,
Marking=s,
)
V_insert_data.save()
并在html中执行此操作
{% for student in Students %}
<tr>
<td colspan="2"><input type="hidden" value="{{student.id}}" name="student">{{student.Students_Enrollment_Records.Students_Enrollment_Records.Students_Enrollment_Records.Student_Users}}</td>
{% for corevalues in behaviorperlevels %}
<td colspan="4">{{corevalues.id}}{{student.id}}
<select name="Marking">
{% for behaviors in behavior %}
<option value="{{behaviors.id}}">{{behaviors.id}}-{{behaviors.Marking}}</option>
{% endfor %}
</select>
</td>
{% endfor %}
</tr>
{% endfor %}
{%用于学生%}
{{学生.学生注册记录.学生注册记录.学生注册记录.学生注册记录.学生用户}
{BehaviorPerLevel%中的corevalues为%0}
{{corevalues.id}{{student.id}}
{%用于行为%]中的行为
{{behaviors.id}-{{behaviors.Marking}
{%endfor%}
{%endfor%}
{%endfor%}
为什么要将核心描述
和标记
放在列表中?填写完列表后,您就可以“按原样”使用它们。为什么不直接使用解压后的值呢<代码>标记[int(i)]-不,将值视为键不是一个好主意。这里的关键是列表中某个值的索引。先生,怎么做?请告诉我你的解决方案。你听说过Django表单和表单集吗?@Carlburbaker这和我的问题有关吗,先生?看起来使用模型表单会很有意义。也许我只是不理解你的代码