From 970611827b0012a40fc0723daadc666224134afa Mon Sep 17 00:00:00 2001 From: Jacob Kaplan-Moss Date: Mon, 21 Jul 2008 15:41:21 +0000 Subject: [PATCH] FIxed #7666: use a bare queryset when accessing single related objects so that the related objects never become inaccessible. git-svn-id: http://code.djangoproject.com/svn/django/trunk@8017 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/db/models/fields/related.py | 3 +- .../reverse_single_related/__init__.py | 0 .../reverse_single_related/models.py | 43 +++++++++++++++++++ 3 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 tests/regressiontests/reverse_single_related/__init__.py create mode 100644 tests/regressiontests/reverse_single_related/models.py diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py index 735dda4969..c76c87ec74 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 0000000000..e69de29bb2 diff --git a/tests/regressiontests/reverse_single_related/models.py b/tests/regressiontests/reverse_single_related/models.py new file mode 100644 index 0000000000..c6bbbebd3d --- /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 + + +"""}