2016-01-25 109 views
2

我在將Django Quiz應用程序集成到我的項目中時不斷收到此錯誤,但不知道如何解決。我可以創建測驗並在列表中看到它們,但只要按下「開始測驗」按鈕,我就會看到這個錯誤。我需要在我的項目中更改哪些內容才能解決此問題?Django屬性錯誤:'int'對象沒有屬性'essay_question' - Django測驗應用程序

以下是完整回溯:

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' 

而這裏的模型作文應用:

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") 

編輯:這裏的問題型號:

@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

任何幫助表示讚賞。

+0

請用'Question' –

+0

@ArashHatami,我更新了它更新您的文章。 – Sportlich

+0

這看起來可能是django_quiz中的一個錯誤,而不是你的代碼(我發現你已經[打開了一個問題](https://github.com/tomwalker/django_quiz/issues/55))。 [tox.ini](https://github.com/tomwalker/django_quiz/blob/master/tox.ini),似乎Django 1.9可能還不被支持。你在Django 1.8.x中測試了你的代碼嗎? – Alasdair

回答

1

翻遍你的代碼,我認爲問題出在你的SittingManager中。你正在做的:

question_set = question_set.values_list('id', flat=True) 

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

我會建議:

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

+0

感謝您的幫助,這有助於解決這個特定的錯誤,但是產生了一些其他的錯誤。@ karthikr建議完全解決了這個問題。無論如何,謝謝,我會用你的建議來處理'question_ids',因爲它更容易理解。 – Sportlich

0

如果你正在運行的Django 1.9,這是一個issue with django-quiz-app-0.5.1,由Sportlich報道。爲了解決這個問題,在測驗/ models.py從開始上線306更改代碼:

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() 

到:

if quiz.random_order is True: 
     question_set = quiz.question_set.all() \ 
             .order_by('?') 
    else: 
     question_set = quiz.question_set.all() 
相關問題