diff options
author | Tom <tom@tomforb.es> | 2017-05-18 19:03:30 +0100 |
---|---|---|
committer | Tim Graham <timograham@gmail.com> | 2017-05-25 09:06:25 -0400 |
commit | bb0b6e526340e638522e093765e534df4e4393d2 (patch) | |
tree | 235f8e2dbb41209b7fd82cc62554549266020695 /django/db/models/query_utils.py | |
parent | 1c3a6cec2ae35c4326971e62acbc1891506e7e72 (diff) | |
download | django-bb0b6e526340e638522e093765e534df4e4393d2.tar.gz |
Fixed #28211 -- Prevented ORing an empty Q() from reducing query join efficiency.
Diffstat (limited to 'django/db/models/query_utils.py')
-rw-r--r-- | django/db/models/query_utils.py | 9 |
1 files changed, 9 insertions, 0 deletions
diff --git a/django/db/models/query_utils.py b/django/db/models/query_utils.py index 27430ae4ca..0fedfac3e2 100644 --- a/django/db/models/query_utils.py +++ b/django/db/models/query_utils.py @@ -5,6 +5,7 @@ Factored out from django.db.models.query to avoid making the main module very large and/or so that they can be used by other modules without getting into circular import difficulties. """ +import copy import functools import inspect from collections import namedtuple @@ -61,6 +62,14 @@ class Q(tree.Node): def _combine(self, other, conn): if not isinstance(other, Q): raise TypeError(other) + + # If the other Q() is empty, ignore it and just use `self`. + if not other: + return copy.deepcopy(self) + # Or if this Q is empty, ignore it and just use `other`. + elif not self: + return copy.deepcopy(other) + obj = type(self)() obj.connector = conn obj.add(self, conn) |