From d95355b6db0a457130bce22bb1dbf1a48731dfc6 Mon Sep 17 00:00:00 2001 From: Jannis Leidel Date: Sun, 22 May 2011 15:21:03 +0000 Subject: [PATCH] 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 --- django/contrib/contenttypes/models.py | 2 +- tests/modeltests/generic_relations/models.py | 8 ++++++++ tests/modeltests/generic_relations/tests.py | 7 ++++++- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/django/contrib/contenttypes/models.py b/django/contrib/contenttypes/models.py index 06e06bcc4e..1f2a3dbbd2 100644 --- a/django/contrib/contenttypes/models.py +++ b/django/contrib/contenttypes/models.py @@ -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) diff --git a/tests/modeltests/generic_relations/models.py b/tests/modeltests/generic_relations/models.py index 18b77a355e..f3e216edf5 100644 --- a/tests/modeltests/generic_relations/models.py +++ b/tests/modeltests/generic_relations/models.py @@ -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() diff --git a/tests/modeltests/generic_relations/tests.py b/tests/modeltests/generic_relations/tests.py index a2bf3bedf6..7596f77fbe 100644 --- a/tests/modeltests/generic_relations/tests.py +++ b/tests/modeltests/generic_relations/tests.py @@ -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"""

""") + 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