mirror of
https://github.com/django/django.git
synced 2025-04-25 17:54:37 +00:00
Fixed #1724 -- updated the output from the shell commands to match what we now
produce. git-svn-id: http://code.djangoproject.com/svn/django/trunk@2937 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
98d6eac81c
commit
ce6f058bbb
@ -445,13 +445,13 @@ Once you're in the shell, explore the database API::
|
|||||||
|
|
||||||
# objects.all() displays all the polls in the database.
|
# objects.all() displays all the polls in the database.
|
||||||
>>> Poll.objects.all()
|
>>> Poll.objects.all()
|
||||||
[<Poll object>]
|
[<Poll: Poll object>]
|
||||||
|
|
||||||
|
|
||||||
Wait a minute. ``<Poll object>`` is, utterly, an unhelpful representation of
|
Wait a minute. ``<Poll: Poll object>`` is, utterly, an unhelpful
|
||||||
this object. Let's fix that by editing the polls model
|
representation of this object. Let's fix that by editing the polls model (in
|
||||||
(in the ``polls/models.py`` file) and adding a ``__str__()`` method to
|
the ``polls/models.py`` file) and adding a ``__str__()`` method to both
|
||||||
both ``Poll`` and ``Choice``::
|
``Poll`` and ``Choice``::
|
||||||
|
|
||||||
class Poll(models.Model):
|
class Poll(models.Model):
|
||||||
# ...
|
# ...
|
||||||
@ -487,30 +487,30 @@ Let's jump back into the Python interactive shell by running
|
|||||||
|
|
||||||
# Make sure our __str__() addition worked.
|
# Make sure our __str__() addition worked.
|
||||||
>>> Poll.objects.all()
|
>>> Poll.objects.all()
|
||||||
[What's up?]
|
[<Poll: What's up?>]
|
||||||
|
|
||||||
# Django provides a rich database lookup API that's entirely driven by
|
# Django provides a rich database lookup API that's entirely driven by
|
||||||
# keyword arguments.
|
# keyword arguments.
|
||||||
>>> Poll.objects.filter(id=1)
|
>>> Poll.objects.filter(id=1)
|
||||||
[What's up?]
|
[<Poll: What's up?>]
|
||||||
>>> Poll.objects.filter(question__startswith='What')
|
>>> Poll.objects.filter(question__startswith='What')
|
||||||
[What's up?]
|
[<Poll: What's up?>]
|
||||||
|
|
||||||
# Get the poll whose year is 2005. Of course, if you're going through this
|
# Get the poll whose year is 2005. Of course, if you're going through this
|
||||||
# tutorial in another year, change as appropriate.
|
# tutorial in another year, change as appropriate.
|
||||||
>>> Poll.objects.get(pub_date__year=2005)
|
>>> Poll.objects.get(pub_date__year=2005)
|
||||||
What's up?
|
<Poll: What's up?>
|
||||||
|
|
||||||
>>> Poll.objects.get(id=2)
|
>>> Poll.objects.get(id=2)
|
||||||
Traceback (most recent call last):
|
Traceback (most recent call last):
|
||||||
...
|
...
|
||||||
DoesNotExist: Poll does not exist for {'id': 2}
|
DoesNotExist: Poll matching query does not exist.
|
||||||
|
|
||||||
# Lookup by a primary key is the most common case, so Django provides a
|
# Lookup by a primary key is the most common case, so Django provides a
|
||||||
# shortcut for primary-key exact lookups.
|
# shortcut for primary-key exact lookups.
|
||||||
# The following is identical to Poll.objects.get(id=1).
|
# The following is identical to Poll.objects.get(id=1).
|
||||||
>>> Poll.objects.get(pk=1)
|
>>> Poll.objects.get(pk=1)
|
||||||
What's up?
|
<Poll: What's up?>
|
||||||
|
|
||||||
# Make sure our custom method worked.
|
# Make sure our custom method worked.
|
||||||
>>> p = Poll.objects.get(pk=1)
|
>>> p = Poll.objects.get(pk=1)
|
||||||
@ -522,18 +522,18 @@ Let's jump back into the Python interactive shell by running
|
|||||||
# of available choices and returns the new Choice object.
|
# of available choices and returns the new Choice object.
|
||||||
>>> p = Poll.objects.get(pk=1)
|
>>> p = Poll.objects.get(pk=1)
|
||||||
>>> p.choice_set.create(choice='Not much', votes=0)
|
>>> p.choice_set.create(choice='Not much', votes=0)
|
||||||
Not much
|
<Choice: Not much>
|
||||||
>>> p.choice_set.create(choice='The sky', votes=0)
|
>>> p.choice_set.create(choice='The sky', votes=0)
|
||||||
The sky
|
<Choice: The sky>
|
||||||
>>> c = p.choice_set.create(choice='Just hacking again', votes=0)
|
>>> c = p.choice_set.create(choice='Just hacking again', votes=0)
|
||||||
|
|
||||||
# Choice objects have API access to their related Poll objects.
|
# Choice objects have API access to their related Poll objects.
|
||||||
>>> c.poll
|
>>> c.poll
|
||||||
What's up?
|
<Poll: What's up?>
|
||||||
|
|
||||||
# And vice versa: Poll objects get access to Choice objects.
|
# And vice versa: Poll objects get access to Choice objects.
|
||||||
>>> p.choice_set.all()
|
>>> p.choice_set.all()
|
||||||
[Not much, The sky, Just hacking again]
|
[<Choice: Not much>, <Choice: The sky>, <Choice: Just hacking again>]
|
||||||
>>> p.choice_set.count()
|
>>> p.choice_set.count()
|
||||||
3
|
3
|
||||||
|
|
||||||
@ -542,7 +542,7 @@ Let's jump back into the Python interactive shell by running
|
|||||||
# This works as many levels deep as you want. There's no limit.
|
# This works as many levels deep as you want. There's no limit.
|
||||||
# Find all Choices for any poll whose pub_date is in 2005.
|
# Find all Choices for any poll whose pub_date is in 2005.
|
||||||
>>> Choice.objects.filter(poll__pub_date__year=2005)
|
>>> Choice.objects.filter(poll__pub_date__year=2005)
|
||||||
[Not much, The sky, Just hacking again]
|
[<Choice: Not much>, <Choice: The sky>, <Choice: Just hacking again>]
|
||||||
|
|
||||||
# Let's delete one of the choices. Use delete() for that.
|
# Let's delete one of the choices. Use delete() for that.
|
||||||
>>> c = p.choice_set.filter(choice__startswith='Just hacking')
|
>>> c = p.choice_set.filter(choice__startswith='Just hacking')
|
||||||
|
Loading…
x
Reference in New Issue
Block a user