Ticket #7510: admin-qs.2.diff

File admin-qs.2.diff, 4.1 KB (added by Alex Gaynor, 15 years ago)
  • django/contrib/admin/options.py

    diff --git a/django/contrib/admin/options.py b/django/contrib/admin/options.py
    index 8cc27c5..7cb1aac 100644
    a b class ModelAdmin(BaseModelAdmin):  
    791791        opts = model._meta
    792792
    793793        try:
    794             obj = model._default_manager.get(pk=unquote(object_id))
     794            obj = self.queryset(request).get(pk=unquote(object_id))
    795795        except model.DoesNotExist:
    796796            # Don't raise Http404 just yet, because we haven't checked
    797797            # permissions yet. We don't want an unauthenticated user to be able
    class ModelAdmin(BaseModelAdmin):  
    976976        app_label = opts.app_label
    977977
    978978        try:
    979             obj = self.model._default_manager.get(pk=unquote(object_id))
     979            obj = self.queryset(request).get(pk=unquote(object_id))
    980980        except self.model.DoesNotExist:
    981981            # Don't raise Http404 just yet, because we haven't checked
    982982            # permissions yet. We don't want an unauthenticated user to be able
  • tests/regressiontests/admin_views/models.py

    diff --git a/tests/regressiontests/admin_views/models.py b/tests/regressiontests/admin_views/models.py
    index 8fc3d38..e2d087d 100644
    a b class ParentAdmin(admin.ModelAdmin):  
    262262    model = Parent
    263263    inlines = [ChildInline]
    264264
     265class EmptyModel(models.Model):
     266    def __unicode__(self):
     267        return "Primary key = %s" % self.id
     268
     269class EmptyModelAdmin(admin.ModelAdmin):
     270    def queryset(self, request):
     271        return super(EmptyModelAdmin, self).queryset(request).filter(pk__gt=1)
     272
    265273admin.site.register(Article, ArticleAdmin)
    266274admin.site.register(CustomArticle, CustomArticleAdmin)
    267275admin.site.register(Section, inlines=[ArticleInline])
    admin.site.register(Subscriber, SubscriberAdmin)  
    274282admin.site.register(ExternalSubscriber, ExternalSubscriberAdmin)
    275283admin.site.register(Podcast, PodcastAdmin)
    276284admin.site.register(Parent, ParentAdmin)
     285admin.site.register(EmptyModel, EmptyModelAdmin)
    277286
    278287# We intentionally register Promo and ChapterXtra1 but not Chapter nor ChapterXtra2.
    279288# That way we cover all four cases:
  • tests/regressiontests/admin_views/tests.py

    diff --git a/tests/regressiontests/admin_views/tests.py b/tests/regressiontests/admin_views/tests.py
    index bba7f6a..447667e 100644
    a b from django.contrib.admin.helpers import ACTION_CHECKBOX_NAME  
    1313from django.utils.html import escape
    1414
    1515# local test models
    16 from models import Article, CustomArticle, Section, ModelWithStringPrimaryKey, Person, Persona, FooAccount, BarAccount, Subscriber, ExternalSubscriber, Podcast
     16from models import Article, CustomArticle, Section, ModelWithStringPrimaryKey, Person, Persona, FooAccount, BarAccount, Subscriber, ExternalSubscriber, Podcast, EmptyModel
    1717
    1818try:
    1919    set
    class TestInlineNotEditable(TestCase):  
    963963        """
    964964        response = self.client.get('/test_admin/admin/admin_views/parent/add/')
    965965        self.failUnlessEqual(response.status_code, 200)
     966
     967
     968class AdminCustomQuerysetTest(TestCase):
     969    fixtures = ['admin-views-users.xml']
     970
     971    def setUp(self):
     972        self.client.login(username='super', password='secret')
     973        self.pks = []
     974        self.pks.append(EmptyModel.objects.create().id)
     975        self.pks.append(EmptyModel.objects.create().id)
     976        self.pks.append(EmptyModel.objects.create().id)
     977
     978    def test_changelist_view(self):
     979        response = self.client.get('/test_admin/admin/admin_views/emptymodel/')
     980        for i in self.pks:
     981            if i > 1:
     982                self.assertContains(response, 'Primary key = %s' % i)
     983            else:
     984                self.assertNotContains(response, 'Primary key = %s' % i)
     985
     986    def test_change_view(self):
     987        for i in self.pks:
     988            response = self.client.get('/test_admin/admin/admin_views/emptymodel/%s/' % i)
     989            if i > 1:
     990                self.assertEqual(response.status_code, 200)
     991            else:
     992                self.assertEqual(response.status_code, 404)
Back to Top