Getting info from all related objects in django
I'm trying to do something pretty simple, but I'm new to Django. I have a quiz system set up for an experiment I'm running.
The relevant entries in models.py follow:
class Flavor(models.Model):
name = models.CharField(max_length=100)
def __unicode__(self):
return self.name
class Passage(models.Model):
name = models.CharField(max_length=100)
def __unicode__(self):
return self.name
class PassageText(models.Model):
passage = models.ForeignKey(Passage)
flavor = models.ForeignKey(Flavor)
contents = models.TextField()
def __unicode__(self):
return "[%s#%s]" % (self.passage, self.flavor)
class Question(models.Model):
passage = models.ForeignKey(Passage)
text = models.TextField()
def __unicode__(self):
return "[%s#%s]" % (self.passage, self.text)
class AnswerOption(models.Model):
question = models.ForeignKey(Question)
text = models.TextField()
is_correct = models.BooleanField(default=False)
def __unicode__(self):
return "[%s#%s]" % (self.question, self.text)
class TestSubject(models.Model):
GENDER_CHOICES = ( ('M','Male'), ('F','Female'), )
EDUCATION_CHOICES = ( ('SH', 'Some high school'), ('HS', 'High school diploma'), ('SC', 'Some college'), ('CD', 'College degree'), ('MD', 'Master\'s degree'), ('PH','PhD or higher education'), )
GPA_CHOICES = ( ('1', '1.0-1.5'), ('2', '1.5-2.0'), ('3', '2.0-2.5'), ('4', '2.5-3.0'), ('5', '3.0-3.5'), ('6', '3.5-4.0'), ('7', '4.0-4.5'), ('8', '4.5-5.0'), )
ip 开发者_C百科= models.CharField(max_length=30)
completed = models.BooleanField(default=False)
created_time = models.DateTimeField(default=datetime.now)
time_used = models.IntegerField(default=0, help_text='number of seconds used for test')
age = models.PositiveIntegerField()
gender = models.CharField(max_length=2, choices=GENDER_CHOICES)
education = models.CharField(max_length=2, choices=EDUCATION_CHOICES)
school = models.CharField(max_length=200)
grad_year = models.PositiveIntegerField()
gpa = models.CharField(max_length=2, choices=GPA_CHOICES)
sat_verbal = models.PositiveIntegerField(blank=True)
sat_math = models.PositiveIntegerField(blank=True)
sat_writing = models.PositiveIntegerField(blank=True)
sat_overall = models.PositiveIntegerField(blank=True)
english_is_your_first_language = models.BooleanField()
kerberos_name_if_applying_for_900_credit = models.CharField(max_length=200, blank=True)
def __unicode__(self):
return "[%s#%s]" % (self.ip, self.created_time)
class TestSequence(models.Model):
subject = models.ForeignKey(TestSubject)
order = models.IntegerField(help_text='1..n')
pt = models.ForeignKey(PassageText)
time_used = models.IntegerField(default=0, help_text='number of seconds used for test')
def __unicode__(self):
return "[%s#%d]" % (self.subject, self.order)
class QuestionSequence(models.Model):
tseq = models.ForeignKey(TestSequence)
order = models.IntegerField(help_text='0..n')
question = models.ForeignKey(Question)
selectedanswer = models.ForeignKey(AnswerOption, blank=True, null=True, default=None)
def __unicode__(self):
return "[%s#%d]" % (self.tseq, self.order)
I want to essentially have a page that just gives me a table of all TestSubject objects (and all their properties) as well as a list of which passages they did and a count of how many questions they got right in that passage.
Well, since no one has helped you yet, here:
# *snip* -- view:
context = { 'test_subjects' : TestSubject.objects.all() }
return render_to_response('Template', context)
# *snip*
# *snip* -- template:
{% for test_subject in test_subjects %}
{{ test_subject.ip }}
{# ... snip ... #}
{% empty %}
There are no test subjects.
{% endfor %}
secondly, it is more preferable to pass a list of "things" to a template and let it do the magic... unless you have some special reason not to do so. Passing them in as a string by "converting" and "joining" them in the view ruins the separation between data and code, and it makes your views "dirty".
For more information on this subject, read: Views, Templates and Models
On a side note, I think it would be better if you rethought some parts of your model design, I see some redundancy there. Storing the test subject's name would be a good and user friendly idea. Finally, shouldn't flavor be flavour? Even if you don't change it in your code, you might want to I18N it for British English speakers.
加载中,请稍侯......
精彩评论