Django

Code

Changeset 2937

Show
Ignore:
Timestamp:
05/18/06 07:41:24 (2 years ago)
Author:
mtredinnick
Message:

Fixed #1724 -- updated the output from the shell commands to match what we now
produce.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/docs/tutorial01.txt

    r2869 r2937  
    446446    # objects.all() displays all the polls in the database. 
    447447    >>> Poll.objects.all() 
    448     [<Poll object>] 
    449  
    450  
    451 Wait a minute. ``<Poll object>`` is, utterly, an unhelpful representation of 
    452 this object. Let's fix that by editing the polls model 
    453 (in the ``polls/models.py`` file) and adding a ``__str__()`` method to 
    454 both ``Poll`` and ``Choice``:: 
     448    [<Poll: Poll object>] 
     449 
     450 
     451Wait a minute. ``<Poll: Poll object>`` is, utterly, an unhelpful 
     452representation of this object. Let's fix that by editing the polls model (in 
     453the ``polls/models.py`` file) and adding a ``__str__()`` method to both 
     454``Poll`` and ``Choice``:: 
    455455 
    456456    class Poll(models.Model): 
     
    488488    # Make sure our __str__() addition worked. 
    489489    >>> Poll.objects.all() 
    490     [What's up?
     490    [<Poll: What's up?>
    491491 
    492492    # Django provides a rich database lookup API that's entirely driven by 
    493493    # keyword arguments. 
    494494    >>> Poll.objects.filter(id=1) 
    495     [What's up?
     495    [<Poll: What's up?>
    496496    >>> Poll.objects.filter(question__startswith='What') 
    497     [What's up?
     497    [<Poll: What's up?>
    498498 
    499499    # Get the poll whose year is 2005. Of course, if you're going through this 
    500500    # tutorial in another year, change as appropriate. 
    501501    >>> Poll.objects.get(pub_date__year=2005) 
    502     What's up? 
     502    <Poll: What's up?> 
    503503 
    504504    >>> Poll.objects.get(id=2) 
    505505    Traceback (most recent call last): 
    506506        ... 
    507     DoesNotExist: Poll does not exist for {'id': 2} 
     507    DoesNotExist: Poll matching query does not exist. 
    508508 
    509509    # Lookup by a primary key is the most common case, so Django provides a 
     
    511511    # The following is identical to Poll.objects.get(id=1). 
    512512    >>> Poll.objects.get(pk=1) 
    513     What's up? 
     513    <Poll: What's up?> 
    514514 
    515515    # Make sure our custom method worked. 
     
    523523    >>> p = Poll.objects.get(pk=1) 
    524524    >>> p.choice_set.create(choice='Not much', votes=0) 
    525     Not much 
     525    <Choice: Not much> 
    526526    >>> p.choice_set.create(choice='The sky', votes=0) 
    527     The sky 
     527    <Choice: The sky> 
    528528    >>> c = p.choice_set.create(choice='Just hacking again', votes=0) 
    529529 
    530530    # Choice objects have API access to their related Poll objects. 
    531531    >>> c.poll 
    532     What's up? 
     532    <Poll: What's up?> 
    533533 
    534534    # And vice versa: Poll objects get access to Choice objects. 
    535535    >>> p.choice_set.all() 
    536     [Not much, The sky, Just hacking again
     536    [<Choice: Not much>, <Choice: The sky>, <Choice: Just hacking again>
    537537    >>> p.choice_set.count() 
    538538    3 
     
    543543    # Find all Choices for any poll whose pub_date is in 2005. 
    544544    >>> Choice.objects.filter(poll__pub_date__year=2005) 
    545     [Not much, The sky, Just hacking again
     545    [<Choice: Not much>, <Choice: The sky>, <Choice: Just hacking again>
    546546 
    547547    # Let's delete one of the choices. Use delete() for that.