Changeset 2937
- Timestamp:
- 05/18/06 07:41:24 (2 years ago)
- Files:
-
- django/trunk/docs/tutorial01.txt (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/trunk/docs/tutorial01.txt
r2869 r2937 446 446 # objects.all() displays all the polls in the database. 447 447 >>> Poll.objects.all() 448 [<Poll object>]449 450 451 Wait a minute. ``<Poll object>`` is, utterly, an unhelpful representation of452 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 451 Wait a minute. ``<Poll: Poll object>`` is, utterly, an unhelpful 452 representation of this object. Let's fix that by editing the polls model (in 453 the ``polls/models.py`` file) and adding a ``__str__()`` method to both 454 ``Poll`` and ``Choice``:: 455 455 456 456 class Poll(models.Model): … … 488 488 # Make sure our __str__() addition worked. 489 489 >>> Poll.objects.all() 490 [ What's up?]490 [<Poll: What's up?>] 491 491 492 492 # Django provides a rich database lookup API that's entirely driven by 493 493 # keyword arguments. 494 494 >>> Poll.objects.filter(id=1) 495 [ What's up?]495 [<Poll: What's up?>] 496 496 >>> Poll.objects.filter(question__startswith='What') 497 [ What's up?]497 [<Poll: What's up?>] 498 498 499 499 # Get the poll whose year is 2005. Of course, if you're going through this 500 500 # tutorial in another year, change as appropriate. 501 501 >>> Poll.objects.get(pub_date__year=2005) 502 What's up?502 <Poll: What's up?> 503 503 504 504 >>> Poll.objects.get(id=2) 505 505 Traceback (most recent call last): 506 506 ... 507 DoesNotExist: Poll does not exist for {'id': 2}507 DoesNotExist: Poll matching query does not exist. 508 508 509 509 # Lookup by a primary key is the most common case, so Django provides a … … 511 511 # The following is identical to Poll.objects.get(id=1). 512 512 >>> Poll.objects.get(pk=1) 513 What's up?513 <Poll: What's up?> 514 514 515 515 # Make sure our custom method worked. … … 523 523 >>> p = Poll.objects.get(pk=1) 524 524 >>> p.choice_set.create(choice='Not much', votes=0) 525 Not much525 <Choice: Not much> 526 526 >>> p.choice_set.create(choice='The sky', votes=0) 527 The sky527 <Choice: The sky> 528 528 >>> c = p.choice_set.create(choice='Just hacking again', votes=0) 529 529 530 530 # Choice objects have API access to their related Poll objects. 531 531 >>> c.poll 532 What's up?532 <Poll: What's up?> 533 533 534 534 # And vice versa: Poll objects get access to Choice objects. 535 535 >>> p.choice_set.all() 536 [ Not much, The sky, Just hacking again]536 [<Choice: Not much>, <Choice: The sky>, <Choice: Just hacking again>] 537 537 >>> p.choice_set.count() 538 538 3 … … 543 543 # Find all Choices for any poll whose pub_date is in 2005. 544 544 >>> 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>] 546 546 547 547 # Let's delete one of the choices. Use delete() for that.
