django: noob question about querying the models
I have the following tables:
class SchoolClass(models.Model):
id = models.A开发者_运维百科utoField(primary_key = True)
class_name = models.TextField()
level = models.IntegerField()
taught_by = models.ManyToManyField(User,related_name="taught_by",through='TeachSubject')
attended_by = models.ManyToManyField(User,related_name='attended_by')
class ConsentFormTemplate(models.Model):
title = models.TextField()
body = models.TextField()
teacher_id = models.ForeignKey(User)
consent_id = models.AutoField(primary_key = True)
class Meta:
db_table = "consent_form_template"
class ConsentForm(models.Model):
student_id = models.ForeignKey(User)
remarks = models.TextField()
#class_id = models.ForeignKey(SchoolClass)
reply = models.NullBooleanField()
parents_remark = models.TextField()
tpl = models.ForeignKey(ConsentFormTemplate)
acknowledged_by = models.ForeignKey(User,related_name='acknowledged_by',null=True,blank=True)
acknowledged_on = models.DateField(auto_now=True,auto_now_add=True,null=True)
date_sent = models.DateField(auto_now=True,auto_now_add =True)
class Meta:
unique_together = ('tpl_id','studentId')
class Meta:
db_table = "consent_form"
Given the pk of the consentformtemplate, how can i construct my ORM query such that my results are as such:
first_name(student) | class_name
consentformtemplate=ConsentFormTemplate.objects.get(pk=consentformtemplate_pk)
consents = ConsentForm.objects.filter(tpl=consentformtemplate).select_related()
for consent in consents:
print "%s | %s" % (consent.student_id.first_name, consent.class_id.class_name)
精彩评论