2006-05-02 09:31:56 +08:00
|
|
|
"""
|
|
|
|
19. OR lookups
|
|
|
|
|
2006-06-04 08:23:51 +08:00
|
|
|
To perform an OR lookup, or a lookup that combines ANDs and ORs,
|
2006-05-02 09:31:56 +08:00
|
|
|
combine QuerySet objects using & and | operators.
|
|
|
|
|
2006-06-04 08:23:51 +08:00
|
|
|
Alternatively, use positional arguments, and pass one or more expressions
|
2006-05-02 09:31:56 +08:00
|
|
|
of clauses using the variable ``django.db.models.Q`` (or any object with
|
|
|
|
a get_sql method).
|
|
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
from django.db import models
|
|
|
|
|
|
|
|
class Article(models.Model):
|
2007-08-05 13:14:46 +08:00
|
|
|
headline = models.CharField(max_length=50)
|
2006-05-02 09:31:56 +08:00
|
|
|
pub_date = models.DateTimeField()
|
2006-06-04 08:23:51 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
class Meta:
|
|
|
|
ordering = ('pub_date',)
|
|
|
|
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
def __unicode__(self):
|
2006-05-02 09:31:56 +08:00
|
|
|
return self.headline
|
|
|
|
|
2006-08-27 21:59:47 +08:00
|
|
|
__test__ = {'API_TESTS':"""
|
2006-05-02 09:31:56 +08:00
|
|
|
>>> from datetime import datetime
|
|
|
|
>>> from django.db.models import Q
|
|
|
|
|
|
|
|
>>> a1 = Article(headline='Hello', pub_date=datetime(2005, 11, 27))
|
|
|
|
>>> a1.save()
|
|
|
|
|
|
|
|
>>> a2 = Article(headline='Goodbye', pub_date=datetime(2005, 11, 28))
|
|
|
|
>>> a2.save()
|
|
|
|
|
|
|
|
>>> a3 = Article(headline='Hello and goodbye', pub_date=datetime(2005, 11, 29))
|
|
|
|
>>> a3.save()
|
|
|
|
|
|
|
|
>>> Article.objects.filter(headline__startswith='Hello') | Article.objects.filter(headline__startswith='Goodbye')
|
2006-06-04 08:23:51 +08:00
|
|
|
[<Article: Hello>, <Article: Goodbye>, <Article: Hello and goodbye>]
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
>>> Article.objects.filter(Q(headline__startswith='Hello') | Q(headline__startswith='Goodbye'))
|
2006-06-04 08:23:51 +08:00
|
|
|
[<Article: Hello>, <Article: Goodbye>, <Article: Hello and goodbye>]
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
>>> Article.objects.filter(Q(headline__startswith='Hello') & Q(headline__startswith='Goodbye'))
|
|
|
|
[]
|
|
|
|
|
2006-05-12 05:51:24 +08:00
|
|
|
# You can shorten this syntax with code like the following,
|
|
|
|
# which is especially useful if building the query in stages:
|
|
|
|
>>> articles = Article.objects.all()
|
|
|
|
>>> articles.filter(headline__startswith='Hello') & articles.filter(headline__startswith='Goodbye')
|
2006-05-02 09:31:56 +08:00
|
|
|
[]
|
|
|
|
|
2006-05-12 05:51:24 +08:00
|
|
|
>>> articles.filter(headline__startswith='Hello') & articles.filter(headline__contains='bye')
|
2006-06-04 08:23:51 +08:00
|
|
|
[<Article: Hello and goodbye>]
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
>>> Article.objects.filter(Q(headline__contains='bye'), headline__startswith='Hello')
|
2006-06-04 08:23:51 +08:00
|
|
|
[<Article: Hello and goodbye>]
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
>>> Article.objects.filter(headline__contains='Hello') | Article.objects.filter(headline__contains='bye')
|
2006-06-04 08:23:51 +08:00
|
|
|
[<Article: Hello>, <Article: Goodbye>, <Article: Hello and goodbye>]
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
>>> Article.objects.filter(headline__iexact='Hello') | Article.objects.filter(headline__contains='ood')
|
2006-06-04 08:23:51 +08:00
|
|
|
[<Article: Hello>, <Article: Goodbye>, <Article: Hello and goodbye>]
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
>>> Article.objects.filter(Q(pk=1) | Q(pk=2))
|
2006-06-04 08:23:51 +08:00
|
|
|
[<Article: Hello>, <Article: Goodbye>]
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
>>> Article.objects.filter(Q(pk=1) | Q(pk=2) | Q(pk=3))
|
2006-06-04 08:23:51 +08:00
|
|
|
[<Article: Hello>, <Article: Goodbye>, <Article: Hello and goodbye>]
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2007-01-04 12:00:16 +08:00
|
|
|
# You could also use "in" to accomplish the same as above.
|
|
|
|
>>> Article.objects.filter(pk__in=[1,2,3])
|
|
|
|
[<Article: Hello>, <Article: Goodbye>, <Article: Hello and goodbye>]
|
|
|
|
|
|
|
|
>>> Article.objects.filter(pk__in=[1,2,3,4])
|
|
|
|
[<Article: Hello>, <Article: Goodbye>, <Article: Hello and goodbye>]
|
|
|
|
|
|
|
|
# Passing "in" an empty list returns no results ...
|
|
|
|
>>> Article.objects.filter(pk__in=[])
|
|
|
|
[]
|
|
|
|
|
|
|
|
# ... but can return results if we OR it with another query.
|
|
|
|
>>> Article.objects.filter(Q(pk__in=[]) | Q(headline__icontains='goodbye'))
|
|
|
|
[<Article: Goodbye>, <Article: Hello and goodbye>]
|
|
|
|
|
2006-06-04 08:23:51 +08:00
|
|
|
# Q arg objects are ANDed
|
2006-05-02 09:31:56 +08:00
|
|
|
>>> Article.objects.filter(Q(headline__startswith='Hello'), Q(headline__contains='bye'))
|
2006-06-04 08:23:51 +08:00
|
|
|
[<Article: Hello and goodbye>]
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
# Q arg AND order is irrelevant
|
|
|
|
>>> Article.objects.filter(Q(headline__contains='bye'), headline__startswith='Hello')
|
2006-06-04 08:23:51 +08:00
|
|
|
[<Article: Hello and goodbye>]
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
# Try some arg queries with operations other than get_list
|
|
|
|
>>> Article.objects.get(Q(headline__startswith='Hello'), Q(headline__contains='bye'))
|
2006-06-04 08:23:51 +08:00
|
|
|
<Article: Hello and goodbye>
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
>>> Article.objects.filter(Q(headline__startswith='Hello') | Q(headline__contains='bye')).count()
|
|
|
|
3
|
|
|
|
|
|
|
|
>>> list(Article.objects.filter(Q(headline__startswith='Hello'), Q(headline__contains='bye')).values())
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
[{'headline': u'Hello and goodbye', 'pub_date': datetime.datetime(2005, 11, 29, 0, 0), 'id': 3}]
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
>>> Article.objects.filter(Q(headline__startswith='Hello')).in_bulk([1,2])
|
2006-06-04 08:23:51 +08:00
|
|
|
{1: <Article: Hello>}
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2006-05-27 07:41:43 +08:00
|
|
|
# Demonstrating exclude with a Q object
|
|
|
|
>>> Article.objects.exclude(Q(headline__startswith='Hello'))
|
2006-06-04 08:23:51 +08:00
|
|
|
[<Article: Goodbye>]
|
2006-05-27 07:41:43 +08:00
|
|
|
|
2006-06-04 08:23:51 +08:00
|
|
|
# The 'complex_filter' method supports framework features such as
|
2006-05-06 08:26:24 +08:00
|
|
|
# 'limit_choices_to' which normally take a single dictionary of lookup arguments
|
|
|
|
# but need to support arbitrary queries via Q objects too.
|
|
|
|
>>> Article.objects.complex_filter({'pk': 1})
|
2006-06-04 08:23:51 +08:00
|
|
|
[<Article: Hello>]
|
2006-05-06 08:26:24 +08:00
|
|
|
>>> Article.objects.complex_filter(Q(pk=1) | Q(pk=2))
|
2006-06-04 08:23:51 +08:00
|
|
|
[<Article: Hello>, <Article: Goodbye>]
|
2006-08-27 21:59:47 +08:00
|
|
|
"""}
|