Fixed #16048 -- Use the base manager instead of the default manager to retrieve a related object of a GenericForeignKey similar to ForeignKeys. Thanks, adurdin.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@16261 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Jannis Leidel 2011-05-22 15:21:03 +00:00
parent fc8116cc4f
commit d95355b6db
3 changed files with 15 additions and 2 deletions

View File

@ -100,7 +100,7 @@ class ContentType(models.Model):
method. The ObjectNotExist exception, if thrown, will not be caught,
so code that calls this method should catch it.
"""
return self.model_class()._default_manager.using(self._state.db).get(**kwargs)
return self.model_class()._base_manager.using(self._state.db).get(**kwargs)
def natural_key(self):
return (self.app_label, self.model)

View File

@ -78,3 +78,11 @@ class Mineral(models.Model):
def __unicode__(self):
return self.name
class GeckoManager(models.Manager):
def get_query_set(self):
return super(GeckoManager, self).get_query_set().filter(has_tail=True)
class Gecko(models.Model):
has_tail = models.BooleanField()
objects = GeckoManager()

View File

@ -4,7 +4,7 @@ from django.contrib.contenttypes.models import ContentType
from django.test import TestCase
from models import (TaggedItem, ValuableTaggedItem, Comparison, Animal,
Vegetable, Mineral)
Vegetable, Mineral, Gecko)
class GenericRelationsTests(TestCase):
@ -223,6 +223,11 @@ class GenericRelationsTests(TestCase):
self.assertEqual(u''.join(form.as_p() for form in formset.forms), u"""<p><label for="id_x-0-tag">Tag:</label> <input id="id_x-0-tag" type="text" name="x-0-tag" maxlength="50" /></p>
<p><label for="id_x-0-DELETE">Delete:</label> <input type="checkbox" name="x-0-DELETE" id="id_x-0-DELETE" /><input type="hidden" name="x-0-id" id="id_x-0-id" /></p>""")
def test_gfk_manager(self):
# GenericForeignKey should not use the default manager (which may filter objects) #16048
tailless = Gecko.objects.create(has_tail=False)
tag = TaggedItem.objects.create(content_object=tailless, tag="lizard")
self.assertEqual(tag.content_object, tailless)
class CustomWidget(forms.CharField):
pass