Ticket #1724: tutorial01.txt.diff

File tutorial01.txt.diff, 3.6 KB (added by Malcolm Tredinnick <malcolm@…>, 19 years ago)

Fix output in examples.

  • docs/tutorial01.txt

     
    44
    55Let's learn by example.
    66
    7 Throughout this tutorial, we'll walk you through the creation of a basic
    8 blogging application.
     7Throughout this tutorial, we'll walk you through the creation of a survey
     8(or poll) application.
    99
    1010It'll consist of two parts:
    1111
    12     * A public site that lets people read your blog entries and submit
    13       comments.
    14     * An admin site that lets you add, change and delete entries and comments.
     12    * A public site that lets people select a choice in each available poll.
     13    * An admin site that lets you add, change and delete questions and possible answers.
    1514
    1615We'll assume you have `Django installed`_ already. You can tell Django is
    1716installed by running the Python interactive interpreter and typing
     
    491490
    492491    >>> from mysite.polls.models import Poll, Choice
    493492
    494     # Make sure our __str__() addition worked.
     493    # This still shows the __repr__ version of the object:
    495494    >>> Poll.objects.all()
    496     [What's up?]
     495    [<Poll object>]
    497496
     497    # Create a helper function to show the question (the __str__ output):
     498    >>> def show(result_list):
     499    ...     return [str(o) for o in result_list]
     500    ...
     501    >>> show(Poll.objects.all())
     502    ["What's up?"]
     503
    498504    # Django provides a rich database lookup API that's entirely driven by
    499505    # keyword arguments.
    500     >>> Poll.objects.filter(id=1)
    501     [What's up?]
    502     >>> Poll.objects.filter(question__startswith='What')
    503     [What's up?]
     506    >>> show(Poll.objects.filter(id=1))
     507    ["What's up?"]
     508    >>> show(Poll.objects.filter(question__startswith='What'))
     509    ["What's up?"]
    504510
    505511    # Get the poll whose year is 2005. Of course, if you're going through this
    506512    # tutorial in another year, change as appropriate.
    507     >>> Poll.objects.get(pub_date__year=2005)
     513    >>> print Poll.objects.get(pub_date__year=2005)
    508514    What's up?
    509515
    510516    >>> Poll.objects.get(id=2)
     
    515521    # Lookup by a primary key is the most common case, so Django provides a
    516522    # shortcut for primary-key exact lookups.
    517523    # The following is identical to Poll.objects.get(id=1).
    518     >>> Poll.objects.get(pk=1)
     524    >>> print Poll.objects.get(pk=1)
    519525    What's up?
    520526
    521527    # Make sure our custom method worked.
     
    528534    # of available choices and returns the new Choice object.
    529535    >>> p = Poll.objects.get(pk=1)
    530536    >>> p.choice_set.create(choice='Not much', votes=0)
    531     Not much
     537    <Choice object>
    532538    >>> p.choice_set.create(choice='The sky', votes=0)
    533     The sky
     539    <Choice object>
    534540    >>> c = p.choice_set.create(choice='Just hacking again', votes=0)
    535541
    536542    # Choice objects have API access to their related Poll objects.
    537     >>> c.poll
     543    >>> print c.poll
    538544    What's up?
    539545
    540546    # And vice versa: Poll objects get access to Choice objects.
    541     >>> p.choice_set.all()
    542     [Not much, The sky, Just hacking again]
     547    >>> show(p.choice_set.all())
     548    ['Not much', 'The sky', 'Just hacking again']
    543549    >>> p.choice_set.count()
    544550    3
    545551
     
    547553    # Use double underscores to separate relationships.
    548554    # This works as many levels deep as you want. There's no limit.
    549555    # Find all Choices for any poll whose pub_date is in 2005.
    550     >>> Choice.objects.filter(poll__pub_date__year=2005)
    551     [Not much, The sky, Just hacking again]
     556    >>> show(Choice.objects.filter(poll__pub_date__year=2005))
     557    ['Not much', 'The sky', 'Just hacking again']
    552558
    553559    # Let's delete one of the choices. Use delete() for that.
    554560    >>> c = p.choice_set.filter(choice__startswith='Just hacking')
Back to Top