diff --git a/django/db/models/fields/related_lookups.py b/django/db/models/fields/related_lookups.py index 5454031c2af..de2564fb0d0 100644 --- a/django/db/models/fields/related_lookups.py +++ b/django/db/models/fields/related_lookups.py @@ -93,7 +93,7 @@ class RelatedLookupMixin(object): # ForeignKey to IntegerField given value 'abc'. The ForeignKey itself # doesn't have validation for non-integers, so we must run validation # using the target field. - if hasattr(self.lhs.output_field, 'get_path_info'): + if self.prepare_rhs and hasattr(self.lhs.output_field, 'get_path_info'): # Get the target field. We can safely assume there is only one # as we don't get to the direct value branch otherwise. target_field = self.lhs.output_field.get_path_info()[-1].target_fields[-1] diff --git a/docs/releases/1.10.1.txt b/docs/releases/1.10.1.txt index d5bfcac2bdc..8f0756bb9eb 100644 --- a/docs/releases/1.10.1.txt +++ b/docs/releases/1.10.1.txt @@ -26,3 +26,6 @@ Bugfixes * Fixed a crash if ``request.META['CONTENT_LENGTH']`` is an empty string (:ticket:`27005`). + +* Fixed the ``isnull`` lookup on a ``ForeignKey`` with its ``to_field`` + pointing to a ``CharField`` (:ticket:`26983`). diff --git a/tests/queries/tests.py b/tests/queries/tests.py index e40ab1fa583..fdfd1b57251 100644 --- a/tests/queries/tests.py +++ b/tests/queries/tests.py @@ -2483,6 +2483,19 @@ class ToFieldTests(TestCase): [node1] ) + def test_isnull_query(self): + apple = Food.objects.create(name="apple") + Eaten.objects.create(food=apple, meal="lunch") + Eaten.objects.create(meal="lunch") + self.assertQuerysetEqual( + Eaten.objects.filter(food__isnull=False), + [''] + ) + self.assertQuerysetEqual( + Eaten.objects.filter(food__isnull=True), + [''] + ) + class ConditionalTests(BaseQuerysetTest): """Tests whose execution depend on different environment conditions like