Ticket #7666: 7666.patch

File 7666.patch, 2.5 KB (added by noodslane, 16 years ago)
  • django/db/models/fields/related.py

    diff -r 1ee4136e46dc -r 4d88b8472b86 django/db/models/fields/related.py
    a b  
    33from django.db.models.fields import AutoField, Field, IntegerField, PositiveIntegerField, PositiveSmallIntegerField, get_ul_class, FieldDoesNotExist
    44from django.db.models.related import RelatedObject
    55from django.db.models.query_utils import QueryWrapper
     6from django.db.models.query import QuerySet
    67from django.utils.text import capfirst
    78from django.utils.translation import ugettext_lazy, string_concat, ungettext, ugettext as _
    89from django.utils.functional import curry
     
    236237                params = {'%s__pk' % self.field.rel.field_name: val}
    237238            else:
    238239                params = {'%s__exact' % self.field.rel.field_name: val}
    239             rel_obj = self.field.rel.to._default_manager.get(**params)
     240            rel_obj = QuerySet(self.field.rel.to).get(**params)
    240241            setattr(instance, cache_name, rel_obj)
    241242            return rel_obj
    242243
  • new file tests/regressiontests/reverse_single_related/models.py

    diff -r 1ee4136e46dc -r 4d88b8472b86 tests/regressiontests/reverse_single_related/models.py
    - +  
     1"""
     2Regression tests for an object that cannot access a single related object due
     3to a restrictive default manager.
     4"""
     5
     6from django.db import models
     7
     8
     9class SourceManager(models.Manager):
     10    def get_query_set(self):
     11        return super(SourceManager, self).get_query_set().filter(is_public=True)
     12
     13class Source(models.Model):
     14    is_public = models.BooleanField()
     15    objects = SourceManager()
     16
     17class Item(models.Model):
     18    source = models.ForeignKey(Source)
     19
     20
     21__test__ = {'API_TESTS':"""
     22
     23>>> public_source = Source.objects.create(is_public=True)
     24>>> public_item = Item.objects.create(source=public_source)
     25
     26>>> private_source = Source.objects.create(is_public=False)
     27>>> private_item = Item.objects.create(source=private_source)
     28
     29Only one source is available via all() due to the custom default manager.
     30
     31>>> Source.objects.all()
     32[<Source: Source object>]
     33
     34>>> public_item.source
     35<Source: Source object>
     36
     37Make sure that an item can still access its related source even if the default
     38manager doesn't normally allow it.
     39
     40>>> private_item.source
     41<Source: Source object>
     42
     43"""}
Back to Top