mirror of
https://github.com/django/django.git
synced 2025-08-04 10:59:45 +00:00
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
This commit is contained in:
parent
0ad8863692
commit
c81d69354a
6 changed files with 139 additions and 14 deletions
|
@ -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()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue