Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #7235 -- EmptyQuerySet no longer raises and exception when it's…

… filter()ed (along with some other QuerySet methods). Thanks, taylormarshall

git-svn-id: http://code.djangoproject.com/svn/django/trunk@12147 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 2dd9a85819218e481f9bc6e88264c2b1ae5f25e8 1 parent 058343c
Adrian Holovaty authored January 09, 2010
75  django/db/models/query.py
@@ -1014,6 +1014,81 @@ def iterator(self):
1014 1014
         # (it raises StopIteration immediately).
1015 1015
         yield iter([]).next()
1016 1016
 
  1017
+    def all(self):
  1018
+        """
  1019
+        Always returns EmptyQuerySet.
  1020
+        """
  1021
+        return self
  1022
+
  1023
+    def filter(self, *args, **kwargs):
  1024
+        """
  1025
+        Always returns EmptyQuerySet.
  1026
+        """
  1027
+        return self
  1028
+
  1029
+    def exclude(self, *args, **kwargs):
  1030
+        """
  1031
+        Always returns EmptyQuerySet.
  1032
+        """
  1033
+        return self
  1034
+
  1035
+    def complex_filter(self, filter_obj):
  1036
+        """
  1037
+        Always returns EmptyQuerySet.
  1038
+        """
  1039
+        return self
  1040
+
  1041
+    def select_related(self, *fields, **kwargs):
  1042
+        """
  1043
+        Always returns EmptyQuerySet.
  1044
+        """
  1045
+        return self
  1046
+
  1047
+    def annotate(self, *args, **kwargs):
  1048
+        """
  1049
+        Always returns EmptyQuerySet.
  1050
+        """
  1051
+        return self
  1052
+
  1053
+    def order_by(self, *field_names):
  1054
+        """
  1055
+        Always returns EmptyQuerySet.
  1056
+        """
  1057
+        return self
  1058
+
  1059
+    def distinct(self, true_or_false=True):
  1060
+        """
  1061
+        Always returns EmptyQuerySet.
  1062
+        """
  1063
+        return self
  1064
+
  1065
+    def extra(self, select=None, where=None, params=None, tables=None,
  1066
+              order_by=None, select_params=None):
  1067
+        """
  1068
+        Always returns EmptyQuerySet.
  1069
+        """
  1070
+        assert self.query.can_filter(), \
  1071
+                "Cannot change a query once a slice has been taken"
  1072
+        return self
  1073
+
  1074
+    def reverse(self):
  1075
+        """
  1076
+        Always returns EmptyQuerySet.
  1077
+        """
  1078
+        return self
  1079
+
  1080
+    def defer(self, *fields):
  1081
+        """
  1082
+        Always returns EmptyQuerySet.
  1083
+        """
  1084
+        return self
  1085
+
  1086
+    def only(self, *fields):
  1087
+        """
  1088
+        Always returns EmptyQuerySet.
  1089
+        """
  1090
+        return self
  1091
+
1017 1092
     # EmptyQuerySet is always an empty result in where-clauses (and similar
1018 1093
     # situations).
1019 1094
     value_annotation = False
35  tests/regressiontests/queries/models.py
@@ -8,7 +8,8 @@
8 8
 
9 9
 from django.conf import settings
10 10
 from django.db import models, DEFAULT_DB_ALIAS
11  
-from django.db.models.query import Q, ITER_CHUNK_SIZE
  11
+from django.db.models import Count
  12
+from django.db.models.query import Q, ITER_CHUNK_SIZE, EmptyQuerySet
12 13
 
13 14
 # Python 2.3 doesn't have sorted()
14 15
 try:
@@ -969,6 +970,38 @@ def __unicode__(self):
969 970
 ...     break
970 971
 True
971 972
 
  973
+Bug #7235 -- an EmptyQuerySet should not raise exceptions if it is filtered.
  974
+>>> q = EmptyQuerySet()
  975
+>>> q.all()
  976
+[]
  977
+>>> q.filter(x=10)
  978
+[]
  979
+>>> q.exclude(y=3)
  980
+[]
  981
+>>> q.complex_filter({'pk': 1})
  982
+[]
  983
+>>> q.select_related('spam', 'eggs')
  984
+[]
  985
+>>> q.annotate(Count('eggs'))
  986
+[]
  987
+>>> q.order_by('-pub_date', 'headline')
  988
+[]
  989
+>>> q.distinct()
  990
+[]
  991
+>>> q.extra(select={'is_recent': "pub_date > '2006-01-01'"})
  992
+[]
  993
+>>> q.query.low_mark = 1
  994
+>>> q.extra(select={'is_recent': "pub_date > '2006-01-01'"})
  995
+Traceback (most recent call last):
  996
+...
  997
+AssertionError: Cannot change a query once a slice has been taken
  998
+>>> q.reverse()
  999
+[]
  1000
+>>> q.defer('spam', 'eggs')
  1001
+[]
  1002
+>>> q.only('spam', 'eggs')
  1003
+[]
  1004
+
972 1005
 Bug #7791 -- there were "issues" when ordering and distinct-ing on fields
973 1006
 related via ForeignKeys.
974 1007
 >>> len(Note.objects.order_by('extrainfo__info').distinct())

0 notes on commit 2dd9a85

Please sign in to comment.
Something went wrong with that request. Please try again.