首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Django属性错误:'int‘对象没有属性'essay_question’- Django测验应用程序

Django属性错误:'int‘对象没有属性'essay_question’- Django测验应用程序
EN

Stack Overflow用户
提问于 2016-01-25 22:49:26
回答 2查看 5.3K关注 0票数 2

当我将Django测验应用集成到我的项目中时,我一直收到这个错误,并且不知道如何解决它。我可以创建测验并在列表中查看它们,但是一旦我按下“开始测验”按钮,我就会收到这个错误。我需要在我的项目中进行哪些更改才能解决此问题?

下面是完整的回溯:

代码语言:javascript
复制
Environment:

Request Method: GET
Request URL: http://127.0.0.1:8000/quizzes/testquiz/take/

Django Version: 1.9.1
Python Version: 2.7.10
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'myapp',
 'essay',
 'quiz',
 'multichoice',
 'true_false',
 'bootstrapform']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback:

File "/home/john/.virtualenvs/lfs/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  149.                     response = self.process_exception_by_middleware(e, request)

File "/home/john/.virtualenvs/lfs/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  147.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/home/john/.virtualenvs/lfs/local/lib/python2.7/site-packages/django/views/generic/base.py" in view
  68.             return self.dispatch(request, *args, **kwargs)

File "/home/john/myapp/quiz/views.py" in dispatch
  148.                                                         self.quiz)

File "/home/john/myapp/quiz/models.py" in user_sitting
  341.             sitting = self.new_sitting(user, quiz)

File "/home/john/myapp/quiz/models.py" in new_sitting
  312.         if len(question_set) == 0:

File "/home/john/.virtualenvs/lfs/local/lib/python2.7/site-packages/django/db/models/query.py" in __len__
  240.         self._fetch_all()

File "/home/john/.virtualenvs/lfs/local/lib/python2.7/site-packages/django/db/models/query.py" in _fetch_all
  1074.             self._result_cache = list(self.iterator())

File "/home/john/.virtualenvs/lfs/local/lib/python2.7/site-packages/model_utils/managers.py" in iterator
  80.                     sub_obj = self._get_sub_obj_recurse(obj, s)

File "/home/john/.virtualenvs/lfs/local/lib/python2.7/site-packages/model_utils/managers.py" in _get_sub_obj_recurse
  153.             node = getattr(obj, rel)

Exception Type: AttributeError at /quizzes/testquiz/take/
Exception Value: 'int' object has no attribute 'essay_question'

以下是essay应用程序的模型:

代码语言:javascript
复制
from __future__ import unicode_literals
from django.utils.encoding import python_2_unicode_compatible
from django.utils.translation import ugettext as _
from quiz.models import Question


@python_2_unicode_compatible
class Essay_Question(Question):

    def check_if_correct(self, guess):
        return False

    def get_answers(self):
        return False

    def get_answers_list(self):
        return False

    def answer_choice_to_string(self, guess):
        return str(guess)

    def __str__(self):
        return self.content

    class Meta:
        verbose_name = _("Essay style question")
        verbose_name_plural = _("Essay style questions")

编辑:问题模型如下:

代码语言:javascript
复制
@python_2_unicode_compatible
class Question(models.Model):
    """
    Base class for all question types.
    Shared properties placed here.
    """

    quiz = models.ManyToManyField(Quiz,
                                  verbose_name=_("Quiz"),
                                  blank=True)

    category = models.ForeignKey(Category,
                                 verbose_name=_("Category"),
                                 blank=True,
                                 null=True)

    sub_category = models.ForeignKey(SubCategory,
                                     verbose_name=_("Sub-Category"),
                                     blank=True,
                                     null=True)

    figure = models.ImageField(upload_to='uploads/%Y/%m/%d',
                               blank=True,
                               null=True,
                               verbose_name=_("Figure"))

    content = models.CharField(max_length=1000,
                               blank=False,
                               help_text=_("Enter the question text that "
                                           "you want displayed"),
                               verbose_name=_('Question'))

    explanation = models.TextField(max_length=2000,
                                   blank=True,
                                   help_text=_("Explanation to be shown "
                                               "after the question has "
                                               "been answered."),
                                   verbose_name=_('Explanation'))

    objects = InheritanceManager()

    class Meta:
        verbose_name = _("Question")
        verbose_name_plural = _("Questions")
        ordering = ['category']

    def __str__(self):
        return self.content

可以在此处找到其他模型和视图:https://github.com/tomwalker/django_quiz

任何帮助都是非常感谢的。

EN

回答 2

Stack Overflow用户

发布于 2016-01-26 01:01:47

纵观您的代码,我认为问题出在您的SittingManager中。您正在执行以下操作:

代码语言:javascript
复制
 question_set = question_set.values_list('id', flat=True)

 if len(question_set) == 0:
    ...

我会推荐:

代码语言:javascript
复制
question_ids = question_set.values_list('id', flat=True)

if question_set.count() == 0:
    ...

# or less performant
# if len(list(question_ids)):
#    ...

并在您的进一步评估中与question_ids合作。有关这方面的更多信息,请浏览文档here

票数 1
EN

Stack Overflow用户

发布于 2016-06-01 22:52:08

如果你运行的是Django1.9,这是一个Sportlich报告的issue with django-quiz-app-0.5.1。要修复它,请将从第306行开始的quiz/models.py中的代码更改为:

代码语言:javascript
复制
    if quiz.random_order is True:
        question_set = quiz.question_set.all() \
                                        .select_subclasses() \
                                        .order_by('?')
    else:
        question_set = quiz.question_set.all() \
                                        .select_subclasses()

至:

代码语言:javascript
复制
    if quiz.random_order is True:
        question_set = quiz.question_set.all() \
                                        .order_by('?')
    else:
        question_set = quiz.question_set.all() 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34995417

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档