2011-02-07 52 views
0

我有以下類:學生,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 

回答

0

它看起來像你指的是哪個models.JournalResponse不應該存在,因爲在班級定義中相同models的名字是指django.db.models

您需要通過實際的模型對象來引用它,所以JournalResponse.objects.filter()

你的情況,你必須JournalResponseStudent反向關係,所以你可以簡單地使用self.journalresponse_set.filter()訪問JournalResponse.objects.filter(student=self) http://docs.djangoproject.com/en/dev/topics/db/queries/#following-relationships-backward

嘗試:

self.journalresponse_set.filter(field__journal=self.assignment) 

另外,你的下一個過濾器行將爲打破以及text!=None。改爲使用exclude(text=None)語法。

+0

問題是沒有錯誤發生。因此,我的問題調試。 – selfsimilar

+1

啊,我的道歉!是的,「model.JournalResponse」行不會立即拋出「AttributeError」,這很奇怪。我會在'status'定義中放入'import pdb; pdb.set_trace()',並開始運行精確查詢。你可以試着直接看我的建議,看看它是否也能正常工作。 –

+0

pdb來拯救,謝​​謝!問題是一個import語句:'from django.contrib.gis.db import models',它重載了模型類的調用。在我從有問題的行中刪除'models.'後,原始代碼工作。現在將嘗試你的建議,關注以下關係。 – selfsimilar