From f83af07ce39573786919b1e7980f3f4e07528531 Mon Sep 17 00:00:00 2001 From: Jacob Kaplan-Moss Date: Wed, 1 Apr 2009 03:11:58 +0000 Subject: [PATCH] Fixed #7510: the ModelAdmin now uses `self.queryset` instead of the default manager. Thanks, Alex Gaynor. git-svn-id: http://code.djangoproject.com/svn/django/trunk@10314 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/contrib/admin/options.py | 4 ++-- tests/regressiontests/admin_views/models.py | 9 +++++++ tests/regressiontests/admin_views/tests.py | 26 ++++++++++++++++++++- 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/django/contrib/admin/options.py b/django/contrib/admin/options.py index 8cc27c5fdc..7cb1aac959 100644 --- a/django/contrib/admin/options.py +++ b/django/contrib/admin/options.py @@ -791,7 +791,7 @@ class ModelAdmin(BaseModelAdmin): opts = model._meta try: - obj = model._default_manager.get(pk=unquote(object_id)) + obj = self.queryset(request).get(pk=unquote(object_id)) except model.DoesNotExist: # Don't raise Http404 just yet, because we haven't checked # permissions yet. We don't want an unauthenticated user to be able @@ -976,7 +976,7 @@ class ModelAdmin(BaseModelAdmin): app_label = opts.app_label try: - obj = self.model._default_manager.get(pk=unquote(object_id)) + obj = self.queryset(request).get(pk=unquote(object_id)) except self.model.DoesNotExist: # Don't raise Http404 just yet, because we haven't checked # permissions yet. We don't want an unauthenticated user to be able diff --git a/tests/regressiontests/admin_views/models.py b/tests/regressiontests/admin_views/models.py index 8fc3d38690..e2d087db42 100644 --- a/tests/regressiontests/admin_views/models.py +++ b/tests/regressiontests/admin_views/models.py @@ -262,6 +262,14 @@ class ParentAdmin(admin.ModelAdmin): model = Parent inlines = [ChildInline] +class EmptyModel(models.Model): + def __unicode__(self): + return "Primary key = %s" % self.id + +class EmptyModelAdmin(admin.ModelAdmin): + def queryset(self, request): + return super(EmptyModelAdmin, self).queryset(request).filter(pk__gt=1) + admin.site.register(Article, ArticleAdmin) admin.site.register(CustomArticle, CustomArticleAdmin) admin.site.register(Section, inlines=[ArticleInline]) @@ -274,6 +282,7 @@ admin.site.register(Subscriber, SubscriberAdmin) admin.site.register(ExternalSubscriber, ExternalSubscriberAdmin) admin.site.register(Podcast, PodcastAdmin) admin.site.register(Parent, ParentAdmin) +admin.site.register(EmptyModel, EmptyModelAdmin) # We intentionally register Promo and ChapterXtra1 but not Chapter nor ChapterXtra2. # That way we cover all four cases: diff --git a/tests/regressiontests/admin_views/tests.py b/tests/regressiontests/admin_views/tests.py index bba7f6ac25..2e228743da 100644 --- a/tests/regressiontests/admin_views/tests.py +++ b/tests/regressiontests/admin_views/tests.py @@ -13,7 +13,7 @@ from django.contrib.admin.helpers import ACTION_CHECKBOX_NAME from django.utils.html import escape # local test models -from models import Article, CustomArticle, Section, ModelWithStringPrimaryKey, Person, Persona, FooAccount, BarAccount, Subscriber, ExternalSubscriber, Podcast +from models import Article, CustomArticle, Section, ModelWithStringPrimaryKey, Person, Persona, FooAccount, BarAccount, Subscriber, ExternalSubscriber, Podcast, EmptyModel try: set @@ -963,3 +963,27 @@ class TestInlineNotEditable(TestCase): """ response = self.client.get('/test_admin/admin/admin_views/parent/add/') self.failUnlessEqual(response.status_code, 200) + + +class AdminCustomQuerysetTest(TestCase): + fixtures = ['admin-views-users.xml'] + + def setUp(self): + self.client.login(username='super', password='secret') + self.pks = [EmptyModel.objects.create().id for i in range(3)] + + def test_changelist_view(self): + response = self.client.get('/test_admin/admin/admin_views/emptymodel/') + for i in self.pks: + if i > 1: + self.assertContains(response, 'Primary key = %s' % i) + else: + self.assertNotContains(response, 'Primary key = %s' % i) + + def test_change_view(self): + for i in self.pks: + response = self.client.get('/test_admin/admin/admin_views/emptymodel/%s/' % i) + if i > 1: + self.assertEqual(response.status_code, 200) + else: + self.assertEqual(response.status_code, 404)