Question.objects.all() isn't returning what's intended
Question:
Hi so I’m coding along to Django’s official guide however I’m having a bit of trouble with my code.
I’m using the Python shell and I’m running the Question.objects.all()
command and it returns with the following:
<QuerySet [<Question: Question object (1)>, <Question: Question object (2)>]>
The answer I’m looking for is <QuerySet [<Question: What's up?>]>
Answers:
The answer I’m looking for is <QuerySet [<Question: What's up?>]>
If your database has two Question
objects, then Question.objects.all()
is never going to return just one result. (Did you really mean you wanted only one result?)
By default, when a model object is printed, it will print the model primary key. If you want different behavior, then as Avinash commented, you need to override the __str__
method in the Question class, like so:
class Question(models.Model):
# fields go here
def __str__(self):
return self.text # or whatever field(s) you want
when you define your model
class Question(models.Model):
id = models.AutoField(primary_key =True)
question = models.CharField(max_length = 250)
def __str__(self):
return self.question
so you need to write an str function like this so that when you run a query the objects will appear as per your requirement.
If you only want one result you can also use get:
Question.objects.get(text='I want this text')
Note that this will raise an error if there are multiple entries that match this criteria. This is why get is typically used with id or some other unique entry.
<QuerySet [<Question: Question object (1)>, <Question: Question object
(2)>]>
The answer I’m looking for is <QuerySet [<Question: What’s up?>]>
Save these changes and start a new Python interactive shell by running python manage.py shell again:
Hi so I’m coding along to Django’s official guide however I’m having a bit of trouble with my code.
I’m using the Python shell and I’m running the Question.objects.all()
command and it returns with the following:
<QuerySet [<Question: Question object (1)>, <Question: Question object (2)>]>
The answer I’m looking for is <QuerySet [<Question: What's up?>]>
The answer I’m looking for is
<QuerySet [<Question: What's up?>]>
If your database has two Question
objects, then Question.objects.all()
is never going to return just one result. (Did you really mean you wanted only one result?)
By default, when a model object is printed, it will print the model primary key. If you want different behavior, then as Avinash commented, you need to override the __str__
method in the Question class, like so:
class Question(models.Model):
# fields go here
def __str__(self):
return self.text # or whatever field(s) you want
when you define your model
class Question(models.Model):
id = models.AutoField(primary_key =True)
question = models.CharField(max_length = 250)
def __str__(self):
return self.question
so you need to write an str function like this so that when you run a query the objects will appear as per your requirement.
If you only want one result you can also use get:
Question.objects.get(text='I want this text')
Note that this will raise an error if there are multiple entries that match this criteria. This is why get is typically used with id or some other unique entry.
<QuerySet [<Question: Question object (1)>, <Question: Question object
(2)>]>The answer I’m looking for is <QuerySet [<Question: What’s up?>]>
Save these changes and start a new Python interactive shell by running python manage.py shell again: