diff options
| author | Russell Keith-Magee <russell@keith-magee.com> | 2006-07-01 01:14:41 +0000 |
|---|---|---|
| committer | Russell Keith-Magee <russell@keith-magee.com> | 2006-07-01 01:14:41 +0000 |
| commit | c81d69354ab44e9b12807af9d544d77149b8ea73 (patch) | |
| tree | 0d2b11260c9c5225b0bd916abecb739c1014d917 /tests | |
| parent | 0ad8863692ca2eb6071fa53bc3db8fcc5513e009 (diff) | |
Fixed #2217 -- Allowed raw objects to be used in __exact and __in query terms. Existing use of primary keys in query terms is preserved.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@3246 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/modeltests/many_to_many/models.py | 23 | ||||
| -rw-r--r-- | tests/modeltests/many_to_one/models.py | 28 | ||||
| -rw-r--r-- | tests/modeltests/one_to_one/models.py | 24 |
3 files changed, 70 insertions, 5 deletions
diff --git a/tests/modeltests/many_to_many/models.py b/tests/modeltests/many_to_many/models.py index e80afece46..0e989a0fbe 100644 --- a/tests/modeltests/many_to_many/models.py +++ b/tests/modeltests/many_to_many/models.py @@ -75,6 +75,10 @@ API_TESTS = """ [<Article: Django lets you build Web apps easily>, <Article: NASA uses Python>] >>> Article.objects.filter(publications__pk=1) [<Article: Django lets you build Web apps easily>, <Article: NASA uses Python>] +>>> Article.objects.filter(publications=1) +[<Article: Django lets you build Web apps easily>, <Article: NASA uses Python>] +>>> Article.objects.filter(publications=p1) +[<Article: Django lets you build Web apps easily>, <Article: NASA uses Python>] >>> Article.objects.filter(publications__title__startswith="Science") [<Article: NASA uses Python>, <Article: NASA uses Python>] @@ -89,6 +93,13 @@ API_TESTS = """ >>> Article.objects.filter(publications__title__startswith="Science").distinct().count() 1 +>>> Article.objects.filter(publications__in=[1,2]).distinct() +[<Article: Django lets you build Web apps easily>, <Article: NASA uses Python>] +>>> Article.objects.filter(publications__in=[1,p2]).distinct() +[<Article: Django lets you build Web apps easily>, <Article: NASA uses Python>] +>>> Article.objects.filter(publications__in=[p1,p2]).distinct() +[<Article: Django lets you build Web apps easily>, <Article: NASA uses Python>] + # Reverse m2m queries are supported (i.e., starting at the table that doesn't # have a ManyToManyField). >>> Publication.objects.filter(id__exact=1) @@ -101,9 +112,19 @@ API_TESTS = """ >>> Publication.objects.filter(article__id__exact=1) [<Publication: The Python Journal>] - >>> Publication.objects.filter(article__pk=1) [<Publication: The Python Journal>] +>>> Publication.objects.filter(article=1) +[<Publication: The Python Journal>] +>>> Publication.objects.filter(article=a1) +[<Publication: The Python Journal>] + +>>> Publication.objects.filter(article__in=[1,2]).distinct() +[<Publication: Highlights for Children>, <Publication: Science News>, <Publication: Science Weekly>, <Publication: The Python Journal>] +>>> Publication.objects.filter(article__in=[1,a2]).distinct() +[<Publication: Highlights for Children>, <Publication: Science News>, <Publication: Science Weekly>, <Publication: The Python Journal>] +>>> Publication.objects.filter(article__in=[a1,a2]).distinct() +[<Publication: Highlights for Children>, <Publication: Science News>, <Publication: Science Weekly>, <Publication: The Python Journal>] # If we delete a Publication, its Articles won't be able to access it. >>> p1.delete() diff --git a/tests/modeltests/many_to_one/models.py b/tests/modeltests/many_to_one/models.py index a830ffbdc2..b7d27e2ed3 100644 --- a/tests/modeltests/many_to_one/models.py +++ b/tests/modeltests/many_to_one/models.py @@ -151,10 +151,20 @@ False [<Article: John's second story>, <Article: This is a test>] # Find all Articles for the Reporter whose ID is 1. +# Use direct ID check, pk check, and object comparison >>> Article.objects.filter(reporter__id__exact=1) [<Article: John's second story>, <Article: This is a test>] >>> Article.objects.filter(reporter__pk=1) [<Article: John's second story>, <Article: This is a test>] +>>> Article.objects.filter(reporter=1) +[<Article: John's second story>, <Article: This is a test>] +>>> Article.objects.filter(reporter=r) +[<Article: John's second story>, <Article: This is a test>] + +>>> Article.objects.filter(reporter__in=[1,2]).distinct() +[<Article: John's second story>, <Article: Paul's story>, <Article: This is a test>] +>>> Article.objects.filter(reporter__in=[r,r2]).distinct() +[<Article: John's second story>, <Article: Paul's story>, <Article: This is a test>] # You need two underscores between "reporter" and "id" -- not one. >>> Article.objects.filter(reporter_id__exact=1) @@ -168,10 +178,6 @@ Traceback (most recent call last): ... TypeError: Cannot resolve keyword 'reporter_id' into field -# "pk" shortcut syntax works in a related context, too. ->>> Article.objects.filter(reporter__pk=1) -[<Article: John's second story>, <Article: This is a test>] - # You can also instantiate an Article by passing # the Reporter's ID instead of a Reporter object. >>> a3 = Article(id=None, headline="This is a test", pub_date=datetime(2005, 7, 27), reporter_id=r.id) @@ -200,6 +206,18 @@ TypeError: Cannot resolve keyword 'reporter_id' into field [<Reporter: John Smith>] >>> Reporter.objects.filter(article__pk=1) [<Reporter: John Smith>] +>>> Reporter.objects.filter(article=1) +[<Reporter: John Smith>] +>>> Reporter.objects.filter(article=a) +[<Reporter: John Smith>] + +>>> Reporter.objects.filter(article__in=[1,4]).distinct() +[<Reporter: John Smith>] +>>> Reporter.objects.filter(article__in=[1,a3]).distinct() +[<Reporter: John Smith>] +>>> Reporter.objects.filter(article__in=[a,a3]).distinct() +[<Reporter: John Smith>] + >>> Reporter.objects.filter(article__headline__startswith='This') [<Reporter: John Smith>, <Reporter: John Smith>, <Reporter: John Smith>] >>> Reporter.objects.filter(article__headline__startswith='This').distinct() @@ -216,6 +234,8 @@ TypeError: Cannot resolve keyword 'reporter_id' into field [<Reporter: John Smith>, <Reporter: John Smith>, <Reporter: John Smith>, <Reporter: John Smith>] >>> Reporter.objects.filter(article__reporter__first_name__startswith='John').distinct() [<Reporter: John Smith>] +>>> Reporter.objects.filter(article__reporter__exact=r).distinct() +[<Reporter: John Smith>] # If you delete a reporter, his articles will be deleted. >>> Article.objects.all() diff --git a/tests/modeltests/one_to_one/models.py b/tests/modeltests/one_to_one/models.py index 27c16b5a34..f95556c08d 100644 --- a/tests/modeltests/one_to_one/models.py +++ b/tests/modeltests/one_to_one/models.py @@ -94,6 +94,12 @@ DoesNotExist: Restaurant matching query does not exist. <Restaurant: Demon Dogs the restaurant> >>> Restaurant.objects.get(place__exact=1) <Restaurant: Demon Dogs the restaurant> +>>> Restaurant.objects.get(place__exact=p1) +<Restaurant: Demon Dogs the restaurant> +>>> Restaurant.objects.get(place=1) +<Restaurant: Demon Dogs the restaurant> +>>> Restaurant.objects.get(place=p1) +<Restaurant: Demon Dogs the restaurant> >>> Restaurant.objects.get(place__pk=1) <Restaurant: Demon Dogs the restaurant> >>> Restaurant.objects.get(place__name__startswith="Demon") @@ -105,8 +111,18 @@ DoesNotExist: Restaurant matching query does not exist. <Place: Demon Dogs the place> >>> Place.objects.get(restaurant__place__exact=1) <Place: Demon Dogs the place> +>>> Place.objects.get(restaurant__place__exact=p1) +<Place: Demon Dogs the place> >>> Place.objects.get(restaurant__pk=1) <Place: Demon Dogs the place> +>>> Place.objects.get(restaurant=1) +<Place: Demon Dogs the place> +>>> Place.objects.get(restaurant=r) +<Place: Demon Dogs the place> +>>> Place.objects.get(restaurant__exact=1) +<Place: Demon Dogs the place> +>>> Place.objects.get(restaurant__exact=r) +<Place: Demon Dogs the place> # Add a Waiter to the Restaurant. >>> w = r.waiter_set.create(name='Joe') @@ -115,14 +131,22 @@ DoesNotExist: Restaurant matching query does not exist. <Waiter: Joe the waiter at Demon Dogs the restaurant> # Query the waiters +>>> Waiter.objects.filter(restaurant__place__pk=1) +[<Waiter: Joe the waiter at Demon Dogs the restaurant>] >>> Waiter.objects.filter(restaurant__place__exact=1) [<Waiter: Joe the waiter at Demon Dogs the restaurant>] +>>> Waiter.objects.filter(restaurant__place__exact=p1) +[<Waiter: Joe the waiter at Demon Dogs the restaurant>] >>> Waiter.objects.filter(restaurant__pk=1) [<Waiter: Joe the waiter at Demon Dogs the restaurant>] >>> Waiter.objects.filter(id__exact=1) [<Waiter: Joe the waiter at Demon Dogs the restaurant>] >>> Waiter.objects.filter(pk=1) [<Waiter: Joe the waiter at Demon Dogs the restaurant>] +>>> Waiter.objects.filter(restaurant=1) +[<Waiter: Joe the waiter at Demon Dogs the restaurant>] +>>> Waiter.objects.filter(restaurant=r) +[<Waiter: Joe the waiter at Demon Dogs the restaurant>] # Delete the restaurant; the waiter should also be removed >>> r = Restaurant.objects.get(pk=1) |
