From e8017bb5c02a4d31b10b5667e9af2f69a22a94fa Mon Sep 17 00:00:00 2001 From: Russell Keith-Magee Date: Mon, 6 Feb 2006 11:21:02 +0000 Subject: [PATCH] magic-removal: Fixed #1305 -- Added new unit test group for reverse lookup queries over foreign keys. Thanks, Andreas. git-svn-id: http://code.djangoproject.com/svn/django/branches/magic-removal@2280 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- tests/modeltests/reverse_lookup/__init__.py | 0 tests/modeltests/reverse_lookup/models.py | 56 +++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 tests/modeltests/reverse_lookup/__init__.py create mode 100644 tests/modeltests/reverse_lookup/models.py diff --git a/tests/modeltests/reverse_lookup/__init__.py b/tests/modeltests/reverse_lookup/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/modeltests/reverse_lookup/models.py b/tests/modeltests/reverse_lookup/models.py new file mode 100644 index 0000000000..2fe77bdbeb --- /dev/null +++ b/tests/modeltests/reverse_lookup/models.py @@ -0,0 +1,56 @@ +""" +25. Reverse lookups + +This demonstrates the reverse lookup features of the database API. +""" + +from django.db import models + +class User(models.Model): + name = models.CharField(maxlength=200) + def __repr__(self): + return self.name + +class Poll(models.Model): + question = models.CharField(maxlength=200) + creator = models.ForeignKey(User) + def __repr__(self): + return self.question + +class Choice(models.Model): + name = models.CharField(maxlength=100) + poll = models.ForeignKey(Poll, related_name="poll_choice") + related_poll = models.ForeignKey(Poll, related_name="related_choice") + def __repr(self): + return self.name + +API_TESTS = """ +>>> 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?") +John Doe +>>> User.objects.get(poll__question__exact="What's the second question?") +Jim Bo + +>>> # Reverse lookups by related_name: +>>> Poll.objects.get(poll_choice__name__exact="This is the answer.") +What's the first question? +>>> Poll.objects.get(related_choice__name__exact="This is the answer.") +What's the second question? + +>>> # 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): + ... +TypeError: Cannot resolve keyword 'choice' into field +"""