Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.0.X] Fixed #7510: the ModelAdmin now uses `self.queryset` instead …

…of the default manager. Thanks, Alex Gaynor. Backport of r10314 from trunk.

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.0.X@10320 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 906f55bf0975e6aa4eb63f4ca71e54baff200bf8 1 parent 53d5f27
Jacob Kaplan-Moss authored April 01, 2009
4  django/contrib/admin/options.py
@@ -556,7 +556,7 @@ def change_view(self, request, object_id, extra_context=None):
556 556
         opts = model._meta
557 557
 
558 558
         try:
559  
-            obj = model._default_manager.get(pk=object_id)
  559
+            obj = self.queryset(request).get(pk=object_id)
560 560
         except model.DoesNotExist:
561 561
             # Don't raise Http404 just yet, because we haven't checked
562 562
             # permissions yet. We don't want an unauthenticated user to be able
@@ -680,7 +680,7 @@ def delete_view(self, request, object_id, extra_context=None):
680 680
         app_label = opts.app_label
681 681
 
682 682
         try:
683  
-            obj = self.model._default_manager.get(pk=object_id)
  683
+            obj = self.queryset(request).get(pk=object_id)
684 684
         except self.model.DoesNotExist:
685 685
             # Don't raise Http404 just yet, because we haven't checked
686 686
             # permissions yet. We don't want an unauthenticated user to be able
9  tests/regressiontests/admin_views/models.py
@@ -191,6 +191,14 @@ class ParentAdmin(admin.ModelAdmin):
191 191
     model = Parent
192 192
     inlines = [ChildInline]
193 193
 
  194
+class EmptyModel(models.Model):
  195
+    def __unicode__(self):
  196
+        return "Primary key = %s" % self.id
  197
+
  198
+class EmptyModelAdmin(admin.ModelAdmin):
  199
+    def queryset(self, request):
  200
+        return super(EmptyModelAdmin, self).queryset(request).filter(pk__gt=1)
  201
+
194 202
 admin.site.register(Article, ArticleAdmin)
195 203
 admin.site.register(CustomArticle, CustomArticleAdmin)
196 204
 admin.site.register(Section, inlines=[ArticleInline])
@@ -199,6 +207,7 @@ class ParentAdmin(admin.ModelAdmin):
199 207
 admin.site.register(Thing, ThingAdmin)
200 208
 admin.site.register(Persona, PersonaAdmin)
201 209
 admin.site.register(Parent, ParentAdmin)
  210
+admin.site.register(EmptyModel, EmptyModelAdmin)
202 211
 
203 212
 # We intentionally register Promo and ChapterXtra1 but not Chapter nor ChapterXtra2.
204 213
 # That way we cover all four cases:
26  tests/regressiontests/admin_views/tests.py
@@ -11,7 +11,7 @@
11 11
 from django.utils.html import escape
12 12
 
13 13
 # local test models
14  
-from models import Article, CustomArticle, Section, ModelWithStringPrimaryKey, Persona, FooAccount, BarAccount
  14
+from models import Article, CustomArticle, Section, ModelWithStringPrimaryKey, Persona, FooAccount, BarAccount, EmptyModel
15 15
 
16 16
 try:
17 17
     set
@@ -836,3 +836,27 @@ def test(self):
836 836
         """
837 837
         response = self.client.get('/test_admin/admin/admin_views/parent/add/')
838 838
         self.failUnlessEqual(response.status_code, 200)
  839
+
  840
+
  841
+class AdminCustomQuerysetTest(TestCase):
  842
+    fixtures = ['admin-views-users.xml']
  843
+
  844
+    def setUp(self):
  845
+        self.client.login(username='super', password='secret')
  846
+        self.pks = [EmptyModel.objects.create().id for i in range(3)]
  847
+
  848
+    def test_changelist_view(self):
  849
+        response = self.client.get('/test_admin/admin/admin_views/emptymodel/')
  850
+        for i in self.pks:
  851
+            if i > 1:
  852
+                self.assertContains(response, 'Primary key = %s' % i)
  853
+            else:
  854
+                self.assertNotContains(response, 'Primary key = %s' % i)
  855
+
  856
+    def test_change_view(self):
  857
+        for i in self.pks:
  858
+            response = self.client.get('/test_admin/admin/admin_views/emptymodel/%s/' % i)
  859
+            if i > 1:
  860
+                self.assertEqual(response.status_code, 200)
  861
+            else:
  862
+                self.assertEqual(response.status_code, 404)

0 notes on commit 906f55b

Please sign in to comment.
Something went wrong with that request. Please try again.