Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django 如何从其他模型函数执行模型查询?_Django_Django Models - Fatal编程技术网

Django 如何从其他模型函数执行模型查询?

Django 如何从其他模型函数执行模型查询?,django,django-models,Django,Django Models,我有以下课程:学生、LabJournal、JournalResponse和JournalField。我想为学生定义一个“状态”函数,以确定他们回答了多少问题(JournalField)(JournalResponse)。问题是函数在以下行中没有返回: total_questions = models.JournalResponse.objects.filter(owner__exact=self.id).filter(field__journal__exact=self.assignment).

我有以下课程:学生、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.')
已更新 以下是工作状态方法:

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

看起来您指的是不应该存在的
models.JournalResponse
(AttributeError?),因为在类定义中,相同的
models
名称指的是
django.db.models

您需要通过实际的模型对象引用它,因此
JournalResponse.objects.filter()

在您的情况下,您与来自
Student
JournalResponse
具有相反的关系,因此您可以简单地使用
self.JournalResponse\u set.filter()
访问
JournalResponse.objects.filter(Student=self)

尝试:


另外,您的下一个过滤行也将在
文本上中断=无
。改用
exclude(text=None)
语法。

问题是不会引发错误。这就是我的问题。啊,我道歉!是的,很奇怪的是,
行建模。JournalResponse
不会立即抛出
属性错误。我会把
导入pdb;pdb.在
状态定义中设置_trace()
,然后开始运行精确的查询。你可以试着直接按照我的建议去做,看看它是否也有效。pdb救命,谢谢!问题在于导入语句:
来自django.contrib.gis.db import models
,它重载了models类调用。在我从有问题的行中删除了
模型。
之后,原来的代码工作了。现在,请尝试您的建议,以反向跟踪关系。不幸的是,调用
self.journalresponse\u set.all()
会出现错误“AttributeError:“'Student'对象没有属性'journalresponse\u set'”。所以反向关系调用似乎因为某些原因不起作用……啊,我明白了
journalresponse\u set
只是django提供的默认名称,如果您没有使用
相关的\u name
覆盖它,那么它将是
self.responses.filter(…)
self.journalresponse_set.filter(field__journal=self.assignment)