#24885 closed Cleanup/optimization (worksforme)
Writing your first Django app, part 1 -> def __str__(self): problem or misunderstanding of doc?
| Reported by: | TitanFighter | Owned by: | TitanFighter |
|---|---|---|---|
| Component: | Documentation | Version: | 1.8 |
| Severity: | Normal | Keywords: | Writing your first Django app, part 1, tutorial, [<Question: Question object>] |
| Cc: | Triage Stage: | Unreviewed | |
| Has patch: | no | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
Hi guys.
I have a problem with the tutorial "Writing your first Django app, part 1" starting from position:
>>> from polls.models import Question, Choice # Make sure our __str__() addition worked. >>> Question.objects.all() [<Question: What's up?>] # Django provides a rich database lookup API that's entirely driven by # keyword arguments. >>> Question.objects.filter(id=1) [<Question: What's up?>] >>> Question.objects.filter(question_text__startswith='What') [<Question: What's up?>]
In all cases I receive:
[<Question: Question object>] instead of [<Question: What's up?>]
I have Ubuntu 15.04_64 and run Python 3.4.3 via "python3 manage.py shell", because Ubuntu has built-in Python 2.*
My code is like:
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): # __unicode__ on Python 2
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): # __unicode__ on Python 2
return self.choice_text
If something wrong with the code, maybe is it possible to edit this section in tutorial to make it a bit clearer? Or is this problem somewhere else?
Thanks.
Change History (3)
comment:1 by , 10 years ago
| Resolution: | → worksforme |
|---|---|
| Status: | new → closed |
comment:3 by , 10 years ago
Hm, I don't see an obvious problem then. Please see TicketClosingReasons/UseSupportChannels for more ways to get help and reopen this ticket if there's something we should add to the docs after you get it working. Thanks!
Did you "start a new Python interactive shell by running
python manage.py shell"? If you use the old one, the new methods won't take effect.