diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py index 9a657d9d26..ae792a30e7 100644 --- a/django/db/models/fields/related.py +++ b/django/db/models/fields/related.py @@ -496,6 +496,8 @@ class ForeignRelatedObjectsDescriptor(object): except (AttributeError, KeyError): db = self._db or router.db_for_read(self.model, instance=self.instance) qs = super(RelatedManager, self).get_query_set().using(db).filter(**self.core_filters) + if getattr(self.instance, attname) is None: + return qs.none() qs._known_related_objects = {rel_field: {self.instance.pk: self.instance}} return qs diff --git a/tests/regressiontests/many_to_one_regress/models.py b/tests/regressiontests/many_to_one_regress/models.py index f3727820f8..0ac0871793 100644 --- a/tests/regressiontests/many_to_one_regress/models.py +++ b/tests/regressiontests/many_to_one_regress/models.py @@ -48,3 +48,9 @@ class Relation(models.Model): def __str__(self): return "%s - %s" % (self.left.category.name, self.right.category.name) + +class Car(models.Model): + make = models.CharField(max_length=100, null=True, unique=True) + +class Driver(models.Model): + car = models.ForeignKey(Car, to_field='make', null=True, related_name='drivers') diff --git a/tests/regressiontests/many_to_one_regress/tests.py b/tests/regressiontests/many_to_one_regress/tests.py index d980d7437c..e140577a49 100644 --- a/tests/regressiontests/many_to_one_regress/tests.py +++ b/tests/regressiontests/many_to_one_regress/tests.py @@ -4,7 +4,8 @@ from django.db import models from django.test import TestCase from django.utils import six -from .models import First, Third, Parent, Child, Category, Record, Relation +from .models import ( + First, Third, Parent, Child, Category, Record, Relation, Car, Driver) class ManyToOneRegressionTests(TestCase): @@ -111,3 +112,25 @@ class ManyToOneRegressionTests(TestCase): # of a model, and interrogate its related field. cat = models.ForeignKey(Category) self.assertEqual('id', cat.rel.get_related_field().name) + + def test_relation_unsaved(self): + # Test that the _set manager does not join on Null value fields (#17541) + Third.objects.create(name='Third 1') + Third.objects.create(name='Third 2') + th = Third(name="testing") + # The object isn't saved an thus the relation field is null - we won't even + # execute a query in this case. + with self.assertNumQueries(0): + self.assertEqual(th.child_set.count(), 0) + th.save() + # Now the model is saved, so we will need to execute an query. + with self.assertNumQueries(1): + self.assertEqual(th.child_set.count(), 0) + + def test_related_null_to_field(self): + c1 = Car.objects.create() + c2 = Car.objects.create() + d1 = Driver.objects.create() + self.assertIs(d1.car, None) + with self.assertNumQueries(0): + self.assertEqual(list(c1.drivers.all()), [])