2005-11-30 14:14:05 +08:00
|
|
|
"""
|
|
|
|
19. OR lookups
|
|
|
|
|
2006-01-31 10:06:42 +08:00
|
|
|
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).
|
|
|
|
|
|
|
|
|
2005-11-30 14:14:05 +08:00
|
|
|
"""
|
|
|
|
|
2005-12-14 13:02:51 +08:00
|
|
|
from django.db import models
|
2005-11-30 14:14:05 +08:00
|
|
|
|
2005-12-14 13:02:51 +08:00
|
|
|
class Article(models.Model):
|
|
|
|
headline = models.CharField(maxlength=50)
|
|
|
|
pub_date = models.DateTimeField()
|
2005-12-21 10:42:27 +08:00
|
|
|
class Meta:
|
2005-11-30 14:14:05 +08:00
|
|
|
ordering = ('pub_date',)
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return self.headline
|
|
|
|
|
|
|
|
API_TESTS = """
|
|
|
|
>>> from datetime import datetime
|
2005-12-14 13:02:51 +08:00
|
|
|
>>> from django.db.models import Q
|
2005-11-30 14:14:05 +08:00
|
|
|
|
2005-12-12 06:10:02 +08:00
|
|
|
>>> a1 = Article(headline='Hello', pub_date=datetime(2005, 11, 27))
|
2005-11-30 14:14:05 +08:00
|
|
|
>>> a1.save()
|
|
|
|
|
2005-12-12 06:10:02 +08:00
|
|
|
>>> a2 = Article(headline='Goodbye', pub_date=datetime(2005, 11, 28))
|
2005-11-30 14:14:05 +08:00
|
|
|
>>> a2.save()
|
|
|
|
|
2005-12-12 06:10:02 +08:00
|
|
|
>>> a3 = Article(headline='Hello and goodbye', pub_date=datetime(2005, 11, 29))
|
2005-11-30 14:14:05 +08:00
|
|
|
>>> a3.save()
|
|
|
|
|
2006-01-31 10:06:42 +08:00
|
|
|
>>> Article.objects.filter(headline__startswith='Hello') | Article.objects.filter(headline__startswith='Goodbye')
|
|
|
|
[Hello, Goodbye, Hello and goodbye]
|
|
|
|
|
|
|
|
>>> Article.objects.filter(Q(headline__startswith='Hello') | Q(headline__startswith='Goodbye'))
|
2005-11-30 14:14:05 +08:00
|
|
|
[Hello, Goodbye, Hello and goodbye]
|
|
|
|
|
2006-01-31 10:06:42 +08:00
|
|
|
>>> Article.objects.filter(Q(headline__startswith='Hello') & Q(headline__startswith='Goodbye'))
|
2005-11-30 14:14:05 +08:00
|
|
|
[]
|
|
|
|
|
2006-01-31 10:06:42 +08:00
|
|
|
>>> Article.objects.filter(headline__startswith='Hello') & Article.objects.filter(headline__startswith='Goodbye')
|
|
|
|
[]
|
|
|
|
|
|
|
|
>>> Article.objects.filter(headline__startswith='Hello') & Article.objects.filter(headline__contains='bye')
|
2005-11-30 14:14:05 +08:00
|
|
|
[Hello and goodbye]
|
|
|
|
|
2006-01-31 10:06:42 +08:00
|
|
|
>>> Article.objects.filter(Q(headline__contains='bye'), headline__startswith='Hello')
|
2005-11-30 14:14:05 +08:00
|
|
|
[Hello and goodbye]
|
|
|
|
|
2006-01-31 10:06:42 +08:00
|
|
|
>>> Article.objects.filter(headline__contains='Hello') | Article.objects.filter(headline__contains='bye')
|
2005-11-30 14:14:05 +08:00
|
|
|
[Hello, Goodbye, Hello and goodbye]
|
|
|
|
|
2006-01-31 10:06:42 +08:00
|
|
|
>>> Article.objects.filter(headline__iexact='Hello') | Article.objects.filter(headline__contains='ood')
|
2005-11-30 14:14:05 +08:00
|
|
|
[Hello, Goodbye, Hello and goodbye]
|
|
|
|
|
2006-01-31 10:06:42 +08:00
|
|
|
>>> Article.objects.filter(Q(pk=1) | Q(pk=2))
|
2005-11-30 14:14:05 +08:00
|
|
|
[Hello, Goodbye]
|
|
|
|
|
2006-01-31 10:06:42 +08:00
|
|
|
>>> Article.objects.filter(Q(pk=1) | Q(pk=2) | Q(pk=3))
|
2005-11-30 14:14:05 +08:00
|
|
|
[Hello, Goodbye, Hello and goodbye]
|
|
|
|
|
2006-01-09 19:01:38 +08:00
|
|
|
# Q arg objects are ANDed
|
2006-01-31 10:06:42 +08:00
|
|
|
>>> Article.objects.filter(Q(headline__startswith='Hello'), Q(headline__contains='bye'))
|
2006-01-09 19:01:38 +08:00
|
|
|
[Hello and goodbye]
|
|
|
|
|
|
|
|
# Q arg AND order is irrelevant
|
2006-01-31 10:06:42 +08:00
|
|
|
>>> Article.objects.filter(Q(headline__contains='bye'), headline__startswith='Hello')
|
2006-01-09 19:01:38 +08:00
|
|
|
[Hello and goodbye]
|
|
|
|
|
|
|
|
# Try some arg queries with operations other than get_list
|
2006-01-31 10:06:42 +08:00
|
|
|
>>> Article.objects.get(Q(headline__startswith='Hello'), Q(headline__contains='bye'))
|
2006-01-09 19:01:38 +08:00
|
|
|
Hello and goodbye
|
|
|
|
|
2006-01-31 10:06:42 +08:00
|
|
|
>>> Article.objects.filter(Q(headline__startswith='Hello') | Q(headline__contains='bye')).count()
|
2006-01-09 19:01:38 +08:00
|
|
|
3
|
|
|
|
|
2006-01-31 10:06:42 +08:00
|
|
|
>>> list(Article.objects.filter(Q(headline__startswith='Hello'), Q(headline__contains='bye')).values())
|
2006-01-09 19:01:38 +08:00
|
|
|
[{'headline': 'Hello and goodbye', 'pub_date': datetime.datetime(2005, 11, 29, 0, 0), 'id': 3}]
|
|
|
|
|
2006-01-31 10:06:42 +08:00
|
|
|
>>> Article.objects.filter(Q(headline__startswith='Hello')).in_bulk([1,2])
|
2006-01-09 19:01:38 +08:00
|
|
|
{1: Hello}
|
|
|
|
|
2005-11-30 14:14:05 +08:00
|
|
|
"""
|