Code

Ticket #12567: 12567.2.diff

File 12567.2.diff, 1.8 KB (added by coleifer, 4 years ago)

updated patch to respect query optimization on isnull=False & inner joins

Line 
1diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py
2index dde1494..f50c219 100644
3--- a/django/db/models/sql/query.py
4+++ b/django/db/models/sql/query.py
5@@ -1015,7 +1015,12 @@ class Query(object):
6 
7         # Process the join list to see if we can remove any inner joins from
8         # the far end (fewer tables in a query is better).
9-        col, alias, join_list = self.trim_joins(target, join_list, last, trim)
10+        if (lookup_type == 'isnull' and value is False and not negate and
11+                len(join_list) > 1):
12+            col = target.column
13+            alias = join_list[-1]
14+        else:
15+            col, alias, join_list = self.trim_joins(target, join_list, last, trim)
16 
17         if connector == OR:
18             # Some joins may need to be promoted when adding a new filter to a
19diff --git a/tests/regressiontests/model_inheritance_regress/models.py b/tests/regressiontests/model_inheritance_regress/models.py
20index c669b23..c3a8c07 100644
21--- a/tests/regressiontests/model_inheritance_regress/models.py
22+++ b/tests/regressiontests/model_inheritance_regress/models.py
23@@ -6,11 +6,6 @@ import datetime
24 
25 from django.db import models
26 
27-# Python 2.3 doesn't have sorted()
28-try:
29-    sorted
30-except NameError:
31-    from django.utils.itercompat import sorted
32 
33 class Place(models.Model):
34     name = models.CharField(max_length=50)
35@@ -385,5 +380,11 @@ AttributeError: 'Person' object has no attribute 'messybachelorparty_set'
36 >>> p4.bachelorparty_set.all()
37 [<BachelorParty: Bachelor party for Bob>, <BachelorParty: Bachelor party for Dave>]
38 
39-"""}
40+# This works
41+>>> Place.objects.exclude(restaurant=None)
42+[<Place:  the place>]
43 
44+# ... and this doesn't.
45+>>> Place.objects.filter(restaurant__isnull=False)
46+[<Place:  the place>]
47+"""}