I am building a student test results template but am having a problem. Currently I have created 2 tests (Maths and Spelling) in my Tests model. The problem is, if I enter the data (scores) for the second test (Spelling) for a student first, the score get incorrectly placed in the template - the score gets placed in the Maths column and not the Spelling column. It gets corrected/moved into the spelling column as soon as I enter the same student’s name in the Maths test. It is a minor issue, but the result still is being put in the wrong place and I don’t know why.
I must be doing something wrong. I am missing some sort of filtering I guess. Hopefully the code below will help explain it further. Thanks.
Models.py
class Student(models.Model):
first = models.CharField(max_length=100)
last = models.CharField(max_length=100)
gender = models.CharField(max_length=1)
teacher = models.ForeignKey(Teacher)
def __unicode__(self):
return "%s %s" % (self.first, self.last)
class Test(models.Model):
name = models.CharField(max_length=100)
Out_of = models.IntegerField(null=True, blank=True)
def __unicode__(self):
return self.name
class Meta:
ordering = ['name']
class Display(models.Model):
name = models.ForeignKey(Student, related_name='art')
test = models.ForeignKey(Test)
one = models.IntegerField(null=True, blank=True)
two = models.IntegerField(null=True, blank=True)
three = models.IntegerField(null=True, blank=True)
# views.py
def test(request):
return list_detail.object_list(
request,
queryset = Student.objects.all(),
template_name = 'display.html',
template_object_name = 'results',
# display.html
<table>
<tr>
<th>First</th>
<th>Name</th>
<th>Maths</th>
<th>Spelling</th>
</tr>
{% for item in results_list %}
<tr>
<td> {{ item.first }} </td>
<td> {{ item.last }} </td>
{% for x in item.art.all %}
<td> {{ x.one }} </td>
{% endfor %}
{% endfor %}
</tr>
</table>
# admin.py
class StudentInline(admin.TabularInline):
model = Display
class TestAdmin(admin.ModelAdmin):
inlines = [StudentInline]