Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

FIxed #7666: use a bare queryset when accessing single related object…

…s so that the related objects never become inaccessible.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@8017 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 970611827b0012a40fc0723daadc666224134afa 1 parent 4016d52
@jacobian jacobian authored
View
3  django/db/models/fields/related.py
@@ -2,6 +2,7 @@
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 @@ def __get__(self, instance, instance_type=None):
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
View
0  tests/regressiontests/reverse_single_related/__init__.py
No changes.
View
43 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()
+[<Source: Source object>]
+
+>>> public_item.source
+<Source: Source object>
+
+Make sure that an item can still access its related source even if the default
+manager doesn't normally allow it.
+
+>>> private_item.source
+<Source: Source object>
+
+"""}
Please sign in to comment.
Something went wrong with that request. Please try again.