2006-05-02 01:31:56 +00:00
|
|
|
"""
|
|
|
|
25. Reverse lookups
|
|
|
|
|
|
|
|
This demonstrates the reverse lookup features of the database API.
|
|
|
|
"""
|
|
|
|
|
|
|
|
from django.db import models
|
|
|
|
|
|
|
|
class User(models.Model):
|
2007-08-05 05:14:46 +00:00
|
|
|
name = models.CharField(max_length=200)
|
2006-06-04 00:23:51 +00:00
|
|
|
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 12:11:04 +00:00
|
|
|
def __unicode__(self):
|
2006-05-02 01:31:56 +00:00
|
|
|
return self.name
|
|
|
|
|
|
|
|
class Poll(models.Model):
|
2007-08-05 05:14:46 +00:00
|
|
|
question = models.CharField(max_length=200)
|
2006-05-02 01:31:56 +00:00
|
|
|
creator = models.ForeignKey(User)
|
2006-06-04 00:23:51 +00:00
|
|
|
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 12:11:04 +00:00
|
|
|
def __unicode__(self):
|
2006-05-02 01:31:56 +00:00
|
|
|
return self.question
|
|
|
|
|
|
|
|
class Choice(models.Model):
|
2007-08-05 05:14:46 +00:00
|
|
|
name = models.CharField(max_length=100)
|
2006-05-02 01:31:56 +00:00
|
|
|
poll = models.ForeignKey(Poll, related_name="poll_choice")
|
|
|
|
related_poll = models.ForeignKey(Poll, related_name="related_choice")
|
2006-06-04 00:23:51 +00:00
|
|
|
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 12:11:04 +00:00
|
|
|
def __unicode__(self):
|
2006-05-02 01:31:56 +00:00
|
|
|
return self.name
|
|
|
|
|
2006-08-27 13:59:47 +00:00
|
|
|
__test__ = {'API_TESTS':"""
|
2006-05-02 01:31:56 +00:00
|
|
|
>>> john = User(name="John Doe")
|
|
|
|
>>> john.save()
|
|
|
|
>>> jim = User(name="Jim Bo")
|
|
|
|
>>> jim.save()
|
|
|
|
>>> first_poll = Poll(question="What's the first question?", creator=john)
|
|
|
|
>>> first_poll.save()
|
|
|
|
>>> second_poll = Poll(question="What's the second question?", creator=jim)
|
|
|
|
>>> second_poll.save()
|
|
|
|
>>> new_choice = Choice(poll=first_poll, related_poll=second_poll, name="This is the answer.")
|
|
|
|
>>> new_choice.save()
|
|
|
|
|
|
|
|
>>> # Reverse lookups by field name:
|
|
|
|
>>> User.objects.get(poll__question__exact="What's the first question?")
|
2006-06-04 00:23:51 +00:00
|
|
|
<User: John Doe>
|
2006-05-02 01:31:56 +00:00
|
|
|
>>> User.objects.get(poll__question__exact="What's the second question?")
|
2006-06-04 00:23:51 +00:00
|
|
|
<User: Jim Bo>
|
2006-05-02 01:31:56 +00:00
|
|
|
|
|
|
|
>>> # Reverse lookups by related_name:
|
|
|
|
>>> Poll.objects.get(poll_choice__name__exact="This is the answer.")
|
2006-06-04 00:23:51 +00:00
|
|
|
<Poll: What's the first question?>
|
2006-05-02 01:31:56 +00:00
|
|
|
>>> Poll.objects.get(related_choice__name__exact="This is the answer.")
|
2006-06-04 00:23:51 +00:00
|
|
|
<Poll: What's the second question?>
|
2006-05-02 01:31:56 +00:00
|
|
|
|
|
|
|
>>> # If a related_name is given you can't use the field name instead:
|
|
|
|
>>> Poll.objects.get(choice__name__exact="This is the answer")
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 02:50:16 +00:00
|
|
|
FieldError: Cannot resolve keyword 'choice' into field. Choices are: creator, id, poll_choice, question, related_choice
|
2006-08-27 13:59:47 +00:00
|
|
|
"""}
|