mirror of
https://github.com/django/django.git
synced 2025-10-30 00:56:09 +00:00
magic-removal: Fixed #1133 -- Added ability to use Q objects as args
in DB lookup queries. git-svn-id: http://code.djangoproject.com/svn/django/branches/magic-removal@1884 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
|
||||
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``.
|
||||
variable ``django.db.models.Q`` (or any object with a get_sql method).
|
||||
"""
|
||||
|
||||
from django.db import models
|
||||
@@ -54,4 +54,33 @@ API_TESTS = """
|
||||
>>> Article.objects.get_list(complex=(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'))
|
||||
[Hello and goodbye]
|
||||
|
||||
# Q arg AND order is irrelevant
|
||||
>>> Article.objects.get_list(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'))
|
||||
Hello and goodbye
|
||||
|
||||
>>> Article.objects.get_count(Q(headline__startswith='Hello') | Q(headline__contains='bye'))
|
||||
3
|
||||
|
||||
>>> Article.objects.get_values(Q(headline__startswith='Hello'), Q(headline__contains='bye'))
|
||||
[{'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'))
|
||||
{1: Hello}
|
||||
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user