Django app Polls: AttributeError: "Choice" object has no "question_text" attribute
I was doing a tutorial on the Django site when I got this error. I am using OS X 10.10.
>>> q.choice_set.create(choice_text='Not much', votes=0)
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/Library/Python/2.7/site-packages/Django-1.7.8-py2.7.egg/django/db/models/base.py", line 458, in __repr__
u = six.text_type(self)
File "/Users/anushrutgupta/Documents/IMG/Django/mysite/polls/models.py", line 22, in __str__
return self.choice_text
AttributeError: 'Choice' object has no attribute 'question_text'
>>>
My .py models:
import datetime
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() -datetime.timedelta(days=1)
class Choice(models.Model):
question = models.ForeignKey(Question)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
def __str__(self):
return self.choice_text
Is there something wrong with the models?
source to share
Looks like a typo in your code on line 22 of models.py
, you have return self.question_text
, but it should be return self.choice_text
.
EDIT : I see you are using Python 2.7, with Python 2 you need to use __unicode__
, not __str__
.
__str__
or__unicode__
?In Python 3, just use
__str__()
.In Python 2, you must define methods
__unicode__()
that return unicode values. Django models have a__str__()
default method that calls__unicode__()
and converts the result to UTF-8. This means itunicode(p)
will return a Unicode string, but itstr(p)
will return a byte string with characters encoded as UTF-8. Python does the opposite: an object has a method__unicode__
that calls__str__
and interprets the result as an ASCII byte string. This difference can be confusing.If this is all gibberish to you, just use Python 3.
source to share
I found a solution to this problem, I was making the same mistake too when you define the variable q,
q = Question.objects.get(pk=1) #it should be like this
q.choice_set.all() #this will show an empty list (only if it empty)
q.choice_set.create(choice_text="sheela ki jawani", votes=0) #and so on
I was doing the first step of incorrectly defining q instead of .get (pk = 1), I used
q = Question.objects.all() #this is wrong
source to share
I am doing the same exercise and based on your code that I was able to complete. (show the question: what?). I have to say that the step-by-step instructions in the tutorial are rather confusing.
My code has two extra lines:
from __future__ import unicode_literals # future between underscores, 2 on each side
and
from django.utils.encoding import python_2_unicode_compatible
source to share
So, the two tables are linked with the foreign key "question"
For the "Question" table to be related to the "Variants" table, you must add a Foreign_eyField parameter named "related_name"
So, to map questions with 1 or n choices, you can use the related name
The code will look like this:
class Choices(models.Model):
question = models.ForeignKey(Question, on_delete=models.CASCADE,
related_name="choice_set")
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
source to share