From 9026e16e30802d831b86d353164966fb5d648eff Mon Sep 17 00:00:00 2001
From: Nate Bragg <jonathan.bragg@alum.rpi.edu>
Date: Tue, 17 Jan 2012 06:47:31 -0500
Subject: [PATCH] My attempt at 17541
---
django/db/models/fields/related.py | 5 ++++-
tests/regressiontests/many_to_one_regress/tests.py | 7 +++++++
2 files changed, 11 insertions(+), 1 deletions(-)
diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py
index 848fd6e..f369666 100644
a
|
b
|
class ForeignRelatedObjectsDescriptor(object):
|
458 | 458 | return self.instance._prefetched_objects_cache[rel_field.related_query_name()] |
459 | 459 | except (AttributeError, KeyError): |
460 | 460 | db = self._db or router.db_for_read(self.model, instance=self.instance) |
461 | | return super(RelatedManager, self).get_query_set().using(db).filter(**self.core_filters) |
| 461 | r = super(RelatedManager, self).get_query_set().using(db).filter(**self.core_filters) |
| 462 | if getattr(self.instance, attname) is None: |
| 463 | return r.none() |
| 464 | return r |
462 | 465 | |
463 | 466 | def get_prefetch_query_set(self, instances): |
464 | 467 | db = self._db or router.db_for_read(self.model) |
diff --git a/tests/regressiontests/many_to_one_regress/tests.py b/tests/regressiontests/many_to_one_regress/tests.py
index 9e04fb4..8c91723 100644
a
|
b
|
class ManyToOneRegressionTests(TestCase):
|
106 | 106 | # of a model, and interrogate its related field. |
107 | 107 | cat = models.ForeignKey(Category) |
108 | 108 | self.assertEqual('id', cat.rel.get_related_field().name) |
| 109 | |
| 110 | def test_related_relation_on_none(self): |
| 111 | # Test that the <field>_set manager does not join on Null value fields (#17541) |
| 112 | Third.objects.create(name='Third 1') |
| 113 | Third.objects.create(name='Third 2') |
| 114 | th = Third(name="testing") |
| 115 | self.assertEqual(th.child_set.count(), 0) |