我有以下类:学生,LabJournal,JournalResponse和JournalField。我想为学生定义一个“状态”功能,以确定他们已经回答了多少个问题(JournalField)(JournalResponse)。问题是功能死亡,没有对下面的行返回:如何从其他模型函数做模型查询?
total_questions = models.JournalResponse.objects.filter(owner__exact=self.id).filter(field__journal__exact=self.assignment).count()
我的猜测是,我做的模型查询错误从类定义之内,或者说不允许你从内查询一个单独的模型。不过,我没有在文档中找到任何证实或否认这一点的内容,并且没有任何错误,因此很难进行调试。运行Django 1.1。下面
代码:
class Student (models.Model):
user = models.ForeignKey(User, unique=True, null=False, related_name='student')
teacher = models.ForeignKey(User, null=False, related_name='students')
assignment = models.ForeignKey(LabJournal, blank=True, null=True, related_name='students')
def get_absolute_url(self):
return "/labjournal/student/%i/" % self.id
def status(self):
if self.assignment == None : return "unassigned"
percent_done = 0
total_questions = models.JournalResponse.objects.filter(owner__exact=self.id).filter(field__journal__exact=self.assignment).count()
answered_questions = models.JournalResponse.objects.filter(owner__exact=self.id).filter(field__journal__exact=self.assignment).filter(text!=None).count()
percent_done = (answered_questions/total_questions)*100
return '%d%% done' % percent_done
class JournalResponse (models.Model):
owner = models.ForeignKey(Student, null=False, related_name='responses')
field = models.ForeignKey(JournalField, null=False, related_name='responses')
text = models.TextField(null=True, blank=True)
file = models.URLField(null=True, blank=True)
class JournalField (models.Model):
TYPE_CHOICES = (
(u'HTML', u'HTML'),
(u'IF', u'ImageField'),
(u'TF', u'TextField'),
)
journal = models.ForeignKey(LabJournal, null=False, related_name='fields', help_text='Parent Journal')
ordinal = models.IntegerField(help_text='Field order')
type = models.CharField(null=False, max_length=64, choices=TYPE_CHOICES, help_text='Field type')
# Contains HTML content for HTML fields, contains the text marked "question" closest
# to and above the current field for picture and text entry fields
content = models.TextField(help_text='Should contain HTML content for HTML (question) fields or associated (previous question) HTML for ImageFields and TextFields.')
修订 这里的工作状态的方法:(?AttributeError的)
def status(self):
if self.assignment == None : return "unassigned"
percent_done = 0
# sets up query, but doesn't actually hit database
response_set = self.responses.filter(owner=self).filter(field__journal=self.assignment)
# force float so divide returns float
# the two count statements are the only two actual hits on the database
total_questions = float(response_set.count())
answered_questions = float(response_set.exclude(text='').count())
percent_done = (answered_questions/total_questions)*100
return '%d%% done' % percent_done
问题是没有错误发生。因此,我的问题调试。 – selfsimilar
啊,我的道歉!是的,“model.JournalResponse”行不会立即抛出“AttributeError”,这很奇怪。我会在'status'定义中放入'import pdb; pdb.set_trace()',并开始运行精确查询。你可以试着直接看我的建议,看看它是否也能正常工作。 –
pdb来拯救,谢谢!问题是一个import语句:'from django.contrib.gis.db import models',它重载了模型类的调用。在我从有问题的行中删除'models.'后,原始代码工作。现在将尝试你的建议,关注以下关系。 – selfsimilar