summaryrefslogtreecommitdiff
path: root/django/db/models/sql
diff options
context:
space:
mode:
authorAnssi Kääriäinen <akaariai@gmail.com>2015-06-25 18:31:07 +0300
committerTim Graham <timograham@gmail.com>2015-06-29 07:49:31 -0400
commit9ed82154bd0bd01c6195942db84302e791ad366f (patch)
tree13e1a31125e71859646e58f9b6c8bb589b1911bd /django/db/models/sql
parent736fb1838cc25f5faf57cd2a4f6b6ab32ff9aadc (diff)
Fixed #23791 -- Corrected object type check for pk__in=qs
When the pk was a relation field, qs.filter(pk__in=qs) didn't work. In addition, fixed Restaurant.objects.filter(place=restaurant_instance), where place is an OneToOneField and the primary key of Restaurant. A big thank you to Josh for review and to Tim for review and cosmetic edits. Thanks to Beauhurst for commissioning the work on this ticket.
Diffstat (limited to 'django/db/models/sql')
-rw-r--r--django/db/models/sql/query.py16
1 files changed, 8 insertions, 8 deletions
diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py
index e88dad536d..df654052fb 100644
--- a/django/db/models/sql/query.py
+++ b/django/db/models/sql/query.py
@@ -18,7 +18,9 @@ from django.db.models.aggregates import Count
from django.db.models.constants import LOOKUP_SEP
from django.db.models.expressions import Col, Ref
from django.db.models.fields.related_lookups import MultiColSource
-from django.db.models.query_utils import Q, PathInfo, refs_expression
+from django.db.models.query_utils import (
+ Q, PathInfo, check_rel_lookup_compatibility, refs_expression,
+)
from django.db.models.sql.constants import (
INNER, LOUTER, ORDER_DIR, ORDER_PATTERN, QUERY_TERMS, SINGLE,
)
@@ -1040,15 +1042,13 @@ class Query(object):
(lookup, self.get_meta().model.__name__))
return lookup_parts, field_parts, False
- def check_query_object_type(self, value, opts):
+ def check_query_object_type(self, value, opts, field):
"""
Checks whether the object passed while querying is of the correct type.
If not, it raises a ValueError specifying the wrong object.
"""
if hasattr(value, '_meta'):
- if not (value._meta.concrete_model == opts.concrete_model
- or opts.concrete_model in value._meta.get_parent_list()
- or value._meta.concrete_model in opts.get_parent_list()):
+ if not check_rel_lookup_compatibility(value._meta.model, opts, field):
raise ValueError(
'Cannot query "%s": Must be "%s" instance.' %
(value, opts.object_name))
@@ -1061,16 +1061,16 @@ class Query(object):
# QuerySets implement is_compatible_query_object_type() to
# determine compatibility with the given field.
if hasattr(value, 'is_compatible_query_object_type'):
- if not value.is_compatible_query_object_type(opts):
+ if not value.is_compatible_query_object_type(opts, field):
raise ValueError(
'Cannot use QuerySet for "%s": Use a QuerySet for "%s".' %
(value.model._meta.model_name, opts.object_name)
)
elif hasattr(value, '_meta'):
- self.check_query_object_type(value, opts)
+ self.check_query_object_type(value, opts, field)
elif hasattr(value, '__iter__'):
for v in value:
- self.check_query_object_type(v, opts)
+ self.check_query_object_type(v, opts, field)
def build_lookup(self, lookups, lhs, rhs):
"""