Error while doing django tutorial part 2

536 views
Skip to first unread message

Kranthi Kiran

unread,
May 26, 2018, 1:19:04 PM5/26/18
to Django users
Hello User,

I am following django tutorial at 

After modifying polls/models.py  and when running python3.6 manage.py shell again


I am getting the following error at the following 

>>> q = Question.objects.get(pk=1)

# Display any choices from the related object set -- none so far.
>>> q.choice_set.all()
<QuerySet []>

Error screenshot




Please help me to resolve the issue at earliest

Kranthi Kiran

unread,
May 26, 2018, 1:47:35 PM5/26/18
to Django users
Text of error message in case the image is not loading


>>> q = Question.objects.get(pk=1)
>>> q.choice_set.all()
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/usr/lib64/python3.6/site-packages/django/db/models/query.py", line 251,                                                                                                                      in __repr__
    return '<%s %r>' % (self.__class__.__name__, data)
  File "/usr/lib64/python3.6/site-packages/django/db/models/base.py", line 513,                                                                                                                      in __repr__
    return '<%s: %s>' % (self.__class__.__name__, self)
  File "/home/kkondapalli/mysite/polls/models.py", line 25, in __str__
    return self.question_text
AttributeError: 'Choice' object has no attribute 'question_text'

Fidel Leon

unread,
May 26, 2018, 2:19:04 PM5/26/18
to django...@googlegroups.com
Check your polls/models.py and verify you have the Question model like that:

class Question(models.Model):
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')
Seems you are missing the question_text attribute.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/0e27456c-eefa-4a1e-bf4f-a44d9e8dcad1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--

Anthony Flury

unread,
May 27, 2018, 2:21:39 AM5/27/18
to django...@googlegroups.com
What is the code for your Choice model.  The error message clearly states that the Choice model is the one with the problem. 

-- 
Anthony Flury
Twitter : @TonyFlury
--

Pravinkumar Kale

unread,
Sep 9, 2018, 6:47:08 PM9/9/18
to Django users

I am also getting same issue...
Following is my code

##########################
from django.db import models
from django.utils import timezone

class Question(models.Model):
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')
    def __str__(self):
        return self.question_text
    def was_published_recently(self):
        return self.pub_date>=timezone.now()
        


class Choice(models.Model):
    question = models.ForeignKey(Question, on_delete=models.CASCADE)
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)
    def __str__(self):
        return self.question_text
#################################

rf

unread,
Jun 1, 2019, 7:25:01 AM6/1/19
to Django users
on last line your code is:
        return self.question_text
it should be:
        return self.choice_text
Reply all
Reply to author
Forward
0 new messages