Ticket #17541: 17541-2.diff

File 17541-2.diff, 1.6 KB (added by claudep, 4 years ago)

Patch with test

  • django/db/models/fields/related.py

    diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py
    index 848fd6e..acd1ff9 100644
    a b class ForeignRelatedObjectsDescriptor(object): 
    458458                    return self.instance._prefetched_objects_cache[rel_field.related_query_name()]
    459459                except (AttributeError, KeyError):
    460460                    db = self._db or router.db_for_read(self.model, instance=self.instance)
     461                    if getattr(self.instance, attname) is None:
     462                        return super(RelatedManager, self).get_query_set().using(db).none()
    461463                    return super(RelatedManager, self).get_query_set().using(db).filter(**self.core_filters)
    462464
    463465            def get_prefetch_query_set(self, instances):
  • tests/regressiontests/many_to_one_regress/tests.py

    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): 
    106106        # of a model, and interrogate its related field.
    107107        cat = models.ForeignKey(Category)
    108108        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)
Back to Top