2011-02-07 10 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()です。

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

また、あなたの次のフィルタラインは、のように破る:あなたのケースでは

、あなたはStudentからJournalResponseとは逆の関係ので、あなたは、単にJournalResponse.objects.filter(student=self) http://docs.djangoproject.com/en/dev/topics/db/queries/#following-relationships-backward

試しにアクセスするためにself.journalresponse_set.filter()を使用することができますがありますよくtext!=Noneにあります。代わりにexclude(text=None)シンタックスを使用してください。

+0

問題は、エラーがスローされないということです。したがって、私の問題はデバッグします。 – selfsimilar

+1

ああ、私の謝罪!ええ、 'models.JournalResponse'という行はすぐに' AttributeError'を投げないことは非常に奇妙です。 'import pdb; pdb.set_trace()'を 'status'定義の中に置き、正確なクエリを実行します。あなたは私の提案にまっすぐに進み、それがうまくいくかどうかを見ることができます。 –

+0

pdbが救助に、ありがとう!問題はimport文です: 'from django.contrib.gis.db import models'は、モデルのクラス呼び出しをオーバーロードしました。問題の行から 'models.'を削除した後、元のコードが機能しました。あなたの関係を後ろ向きにするようにあなたの提案を試してみましょう。 – selfsimilar

関連する問題