Hi folks,
I am a beginner starting with Django and following tutorial 5. In the test case test_no_questions, I encountered the following error:
"Traceback (most recent call last):
File "C:\Users\Varsh\OneDrive\Documents\django\mysite\polls\tests.py", line 39, in test_no_questions
self.assertQuerySetEqual(response.context["latest_question_list"], [])
File "C:\Users\Varsh\OneDrive\Documents\django\venv\Lib\site-packages\django\test\testcases.py", line 1260, in assertQuerySetEqual
return self.assertEqual(list(items), values, msg=msg)
^^^^^^^^^^^
TypeError: 'method' object is not iterable"
Below is my IndexView implementation:
class IndexView(generic.ListView):
template_name = "polls/index.html"
context_object_name = "latest_question_list"
def queryset(self):
"""Return the last five published questions."""
return Question.objects.filter(pub_date__lte = timezone.now()).order_by("-pub_date")[:5]
And here is the relevant part of my IndexViewTest:
def create_question(question_text,days):
time = timezone.now() + datetime.timedelta(days=days)
return Question.objects.create(question_text=question_text,pub_date=time)
class QuestionIndexViewTest(TestCase):
def test_no_questions(self):
""" If no questions present, appropriate message should be displayed"""
response = self.client.get(reverse("polls:index"))
self.assertEqual(response.status_code,200)
self.assertContains(response,"There are no polls available")
self.assertQuerySetEqual(response.context["latest_question_list"](), [])
# self.assertQuerySetEqual(response.context["latest_question_list"],[])
Interestingly, the test works when I use response.context["latest_question_list"] directly, which suggests that response.context["latest_question_list"] is a method. Have I misunderstood something, or has the implementation of the queryset method changed in Django?