Code

Ticket #2473: where_false.diff

File where_false.diff, 1.7 KB (added by Gary Wilson <gary.wilson@…>, 8 years ago)

using WHERE (false) when empty, with the tests

Line 
1=== modified file 'django/db/models/query.py'
2--- django/db/models/query.py   2006-12-19 04:35:09 +0000
3+++ django/db/models/query.py   2006-12-20 03:23:03 +0000
4@@ -641,7 +641,11 @@
5     except KeyError:
6         pass
7     if lookup_type == 'in':
8-        return '%s%s IN (%s)' % (table_prefix, field_name, ','.join(['%s' for v in value]))
9+        in_string = ','.join(['%s' for id in value])
10+        if in_string:
11+            return '%s%s IN (%s)' % (table_prefix, field_name, in_string)
12+        else:
13+            return 'false'
14     elif lookup_type == 'range':
15         return '%s%s BETWEEN %%s AND %%s' % (table_prefix, field_name)
16     elif lookup_type in ('year', 'month', 'day'):
17
18=== modified file 'tests/modeltests/or_lookups/models.py'
19--- tests/modeltests/or_lookups/models.py       2006-12-19 04:35:09 +0000
20+++ tests/modeltests/or_lookups/models.py       2006-12-19 06:49:47 +0000
21@@ -69,6 +69,21 @@
22 >>> Article.objects.filter(Q(pk=1) | Q(pk=2) | Q(pk=3))
23 [<Article: Hello>, <Article: Goodbye>, <Article: Hello and goodbye>]
24 
25+# You could also use "in" to accomplish the same as above.
26+>>> Article.objects.filter(pk__in=[1,2,3])
27+[<Article: Hello>, <Article: Goodbye>, <Article: Hello and goodbye>]
28+
29+>>> Article.objects.filter(pk__in=[1,2,3,4])
30+[<Article: Hello>, <Article: Goodbye>, <Article: Hello and goodbye>]
31+
32+# Passing "in" an empty list returns no results.
33+>>> Article.objects.filter(pk__in=[])
34+[]
35+
36+# But can return results if we OR it with another query.
37+>>> Article.objects.filter(Q(pk__in=[]) | Q(headline__icontains='goodbye'))
38+[<Article: Goodbye>, <Article: Hello and goodbye>]
39+
40 # Q arg objects are ANDed
41 >>> Article.objects.filter(Q(headline__startswith='Hello'), Q(headline__contains='bye'))
42 [<Article: Hello and goodbye>]
43