diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py index 735dda49690..c76c87ec74b 100644 --- a/django/db/models/fields/related.py +++ b/django/db/models/fields/related.py @@ -2,6 +2,7 @@ from django.db import connection, transaction from django.db.models import signals, get_model from django.db.models.fields import AutoField, Field, IntegerField, PositiveIntegerField, PositiveSmallIntegerField, FieldDoesNotExist from django.db.models.related import RelatedObject +from django.db.models.query import QuerySet from django.db.models.query_utils import QueryWrapper from django.utils.text import capfirst from django.utils.translation import ugettext_lazy, string_concat, ungettext, ugettext as _ @@ -236,7 +237,7 @@ class ReverseSingleRelatedObjectDescriptor(object): params = {'%s__pk' % self.field.rel.field_name: val} else: params = {'%s__exact' % self.field.rel.field_name: val} - rel_obj = self.field.rel.to._default_manager.get(**params) + rel_obj = QuerySet(self.field.rel.to).get(**params) setattr(instance, cache_name, rel_obj) return rel_obj diff --git a/tests/regressiontests/reverse_single_related/__init__.py b/tests/regressiontests/reverse_single_related/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/regressiontests/reverse_single_related/models.py b/tests/regressiontests/reverse_single_related/models.py new file mode 100644 index 00000000000..c6bbbebd3d5 --- /dev/null +++ b/tests/regressiontests/reverse_single_related/models.py @@ -0,0 +1,43 @@ +""" +Regression tests for an object that cannot access a single related object due +to a restrictive default manager. +""" + +from django.db import models + + +class SourceManager(models.Manager): + def get_query_set(self): + return super(SourceManager, self).get_query_set().filter(is_public=True) + +class Source(models.Model): + is_public = models.BooleanField() + objects = SourceManager() + +class Item(models.Model): + source = models.ForeignKey(Source) + + +__test__ = {'API_TESTS':""" + +>>> public_source = Source.objects.create(is_public=True) +>>> public_item = Item.objects.create(source=public_source) + +>>> private_source = Source.objects.create(is_public=False) +>>> private_item = Item.objects.create(source=private_source) + +Only one source is available via all() due to the custom default manager. + +>>> Source.objects.all() +[] + +>>> public_item.source + + +Make sure that an item can still access its related source even if the default +manager doesn't normally allow it. + +>>> private_item.source + + +"""}