1
0
mirror of https://github.com/django/django.git synced 2025-04-14 20:34:36 +00:00

magic-removal: updated syntax of 'or_lookup' model tests, including

logical combinations of both QuerySets and Q objects


git-svn-id: http://code.djangoproject.com/svn/django/branches/magic-removal@2199 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Luke Plant 2006-01-31 02:06:42 +00:00
parent b72d8da3b2
commit 2983bd8989

View File

@ -1,9 +1,14 @@
"""
19. OR lookups
To perform an OR lookup, or a lookup that combines ANDs and ORs, use the
``complex`` keyword argument, and pass it an expression of clauses using the
variable ``django.db.models.Q`` (or any object with a get_sql method).
To perform an OR lookup, or a lookup that combines ANDs and ORs,
combine QuerySet objects using & and | operators.
Alternatively, use positional arguments, and pass one or more expressions
of clauses using the variable ``django.db.models.Q`` (or any object with
a get_sql method).
"""
from django.db import models
@ -30,57 +35,55 @@ API_TESTS = """
>>> a3 = Article(headline='Hello and goodbye', pub_date=datetime(2005, 11, 29))
>>> a3.save()
>>> Article.objects.get_list(complex=(Q(headline__startswith='Hello') | Q(headline__startswith='Goodbye')))
>>> Article.objects.filter(headline__startswith='Hello') | Article.objects.filter(headline__startswith='Goodbye')
[Hello, Goodbye, Hello and goodbye]
>>> Article.objects.get_list(complex=(Q(headline__startswith='Hello') & Q(headline__startswith='Goodbye')))
>>> Article.objects.filter(Q(headline__startswith='Hello') | Q(headline__startswith='Goodbye'))
[Hello, Goodbye, Hello and goodbye]
>>> Article.objects.filter(Q(headline__startswith='Hello') & Q(headline__startswith='Goodbye'))
[]
>>> Article.objects.get_list(complex=(Q(headline__startswith='Hello') & Q(headline__contains='bye')))
>>> Article.objects.filter(headline__startswith='Hello') & Article.objects.filter(headline__startswith='Goodbye')
[]
>>> Article.objects.filter(headline__startswith='Hello') & Article.objects.filter(headline__contains='bye')
[Hello and goodbye]
>>> Article.objects.get_list(headline__startswith='Hello', complex=Q(headline__contains='bye'))
>>> Article.objects.filter(Q(headline__contains='bye'), headline__startswith='Hello')
[Hello and goodbye]
>>> Article.objects.get_list(complex=(Q(headline__contains='Hello') | Q(headline__contains='bye')))
>>> Article.objects.filter(headline__contains='Hello') | Article.objects.filter(headline__contains='bye')
[Hello, Goodbye, Hello and goodbye]
>>> Article.objects.get_list(complex=(Q(headline__iexact='Hello') | Q(headline__contains='ood')))
>>> Article.objects.filter(headline__iexact='Hello') | Article.objects.filter(headline__contains='ood')
[Hello, Goodbye, Hello and goodbye]
>>> Article.objects.get_list(complex=(Q(pk=1) | Q(pk=2)))
>>> Article.objects.filter(Q(pk=1) | Q(pk=2))
[Hello, Goodbye]
>>> Article.objects.get_list(complex=(Q(pk=1) | Q(pk=2) | Q(pk=3)))
>>> Article.objects.filter(Q(pk=1) | Q(pk=2) | Q(pk=3))
[Hello, Goodbye, Hello and goodbye]
# Queries can use Q objects as args
>>> Article.objects.get_list(Q(headline__startswith='Hello'))
[Hello, Hello and goodbye]
# Q arg objects are ANDed
>>> Article.objects.get_list(Q(headline__startswith='Hello'), Q(headline__contains='bye'))
>>> Article.objects.filter(Q(headline__startswith='Hello'), Q(headline__contains='bye'))
[Hello and goodbye]
# Q arg AND order is irrelevant
>>> Article.objects.get_list(Q(headline__contains='bye'), headline__startswith='Hello')
>>> Article.objects.filter(Q(headline__contains='bye'), headline__startswith='Hello')
[Hello and goodbye]
# QOrs are ok, as they ultimately resolve to a Q
>>> Article.objects.get_list(Q(headline__contains='Hello') | Q(headline__contains='bye'))
[Hello, Goodbye, Hello and goodbye]
# Try some arg queries with operations other than get_list
>>> Article.objects.get_object(Q(headline__startswith='Hello'), Q(headline__contains='bye'))
>>> Article.objects.get(Q(headline__startswith='Hello'), Q(headline__contains='bye'))
Hello and goodbye
>>> Article.objects.get_count(Q(headline__startswith='Hello') | Q(headline__contains='bye'))
>>> Article.objects.filter(Q(headline__startswith='Hello') | Q(headline__contains='bye')).count()
3
>>> Article.objects.get_values(Q(headline__startswith='Hello'), Q(headline__contains='bye'))
>>> list(Article.objects.filter(Q(headline__startswith='Hello'), Q(headline__contains='bye')).values())
[{'headline': 'Hello and goodbye', 'pub_date': datetime.datetime(2005, 11, 29, 0, 0), 'id': 3}]
>>> Article.objects.get_in_bulk([1,2], Q(headline__startswith='Hello'))
>>> Article.objects.filter(Q(headline__startswith='Hello')).in_bulk([1,2])
{1: Hello}
"""