Ticket #1724: tutorial01.txt.diff
File tutorial01.txt.diff, 3.6 KB (added by , 19 years ago) |
---|
-
docs/tutorial01.txt
4 4 5 5 Let's learn by example. 6 6 7 Throughout this tutorial, we'll walk you through the creation of a basic8 bloggingapplication.7 Throughout this tutorial, we'll walk you through the creation of a survey 8 (or poll) application. 9 9 10 10 It'll consist of two parts: 11 11 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. 15 14 16 15 We'll assume you have `Django installed`_ already. You can tell Django is 17 16 installed by running the Python interactive interpreter and typing … … 491 490 492 491 >>> from mysite.polls.models import Poll, Choice 493 492 494 # Make sure our __str__() addition worked.493 # This still shows the __repr__ version of the object: 495 494 >>> Poll.objects.all() 496 [ What's up?]495 [<Poll object>] 497 496 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 498 504 # Django provides a rich database lookup API that's entirely driven by 499 505 # 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?"] 504 510 505 511 # Get the poll whose year is 2005. Of course, if you're going through this 506 512 # tutorial in another year, change as appropriate. 507 >>> Poll.objects.get(pub_date__year=2005)513 >>> print Poll.objects.get(pub_date__year=2005) 508 514 What's up? 509 515 510 516 >>> Poll.objects.get(id=2) … … 515 521 # Lookup by a primary key is the most common case, so Django provides a 516 522 # shortcut for primary-key exact lookups. 517 523 # The following is identical to Poll.objects.get(id=1). 518 >>> Poll.objects.get(pk=1)524 >>> print Poll.objects.get(pk=1) 519 525 What's up? 520 526 521 527 # Make sure our custom method worked. … … 528 534 # of available choices and returns the new Choice object. 529 535 >>> p = Poll.objects.get(pk=1) 530 536 >>> p.choice_set.create(choice='Not much', votes=0) 531 Not much537 <Choice object> 532 538 >>> p.choice_set.create(choice='The sky', votes=0) 533 The sky539 <Choice object> 534 540 >>> c = p.choice_set.create(choice='Just hacking again', votes=0) 535 541 536 542 # Choice objects have API access to their related Poll objects. 537 >>> c.poll543 >>> print c.poll 538 544 What's up? 539 545 540 546 # 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'] 543 549 >>> p.choice_set.count() 544 550 3 545 551 … … 547 553 # Use double underscores to separate relationships. 548 554 # This works as many levels deep as you want. There's no limit. 549 555 # 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'] 552 558 553 559 # Let's delete one of the choices. Use delete() for that. 554 560 >>> c = p.choice_set.filter(choice__startswith='Just hacking')