Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

EmptyQuerySet classes can now be merged with normal querysets.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@7765 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 9e23c3c5d9d796540356906399365c651e07f084 1 parent 0e692fd
@malcolmt malcolmt authored
Showing with 20 additions and 0 deletions.
  1. +10 −0 django/db/models/query.py
  2. +10 −0 tests/regressiontests/queries/models.py
View
10 django/db/models/query.py
@@ -218,6 +218,8 @@ def __getitem__(self, k):
def __and__(self, other):
self._merge_sanity_check(other)
+ if isinstance(other, EmptyQuerySet):
+ return other._clone()
combined = self._clone()
combined.query.combine(other.query, sql.AND)
return combined
@@ -225,6 +227,8 @@ def __and__(self, other):
def __or__(self, other):
self._merge_sanity_check(other)
combined = self._clone()
+ if isinstance(other, EmptyQuerySet):
+ return combined
combined.query.combine(other.query, sql.OR)
return combined
@@ -705,6 +709,12 @@ def __init__(self, model=None, query=None):
super(EmptyQuerySet, self).__init__(model, query)
self._result_cache = []
+ def __and__(self, other):
+ return self._clone()
+
+ def __or__(self, other):
+ return other._clone()
+
def count(self):
return 0
View
10 tests/regressiontests/queries/models.py
@@ -781,5 +781,15 @@ class Child(models.Model):
>>> Valid.objects.all()
[]
+Empty querysets can be merged with others.
+>>> Note.objects.none() | Note.objects.all()
+[<Note: n1>, <Note: n2>, <Note: n3>]
+>>> Note.objects.all() | Note.objects.none()
+[<Note: n1>, <Note: n2>, <Note: n3>]
+>>> Note.objects.none() & Note.objects.all()
+[]
+>>> Note.objects.all() & Note.objects.none()
+[]
+
"""}
Please sign in to comment.
Something went wrong with that request. Please try again.