Fixed #26983 -- Fixed isnull filtering on ForeignKey with to_field

Thanks weidwonder for the report.
This commit is contained in:
Claude Paroz 2016-08-02 17:27:45 +02:00
parent 7bc5274f6f
commit 272eccf7ff
3 changed files with 17 additions and 1 deletions

View File

@ -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]

View File

@ -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`).

View File

@ -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),
['<Eaten: apple at lunch>']
)
self.assertQuerysetEqual(
Eaten.objects.filter(food__isnull=True),
['<Eaten: None at lunch>']
)
class ConditionalTests(BaseQuerysetTest):
"""Tests whose execution depend on different environment conditions like