Ticket #7510: admin-qs.diff

File admin-qs.diff, 4.0 KB (added by Alex Gaynor, 16 years ago)

Patch with tests

  • django/contrib/admin/options.py

    diff --git a/django/contrib/admin/options.py b/django/contrib/admin/options.py
    index 501014a..41b0412 100644
    a b class ModelAdmin(BaseModelAdmin):  
    523523        app_label = opts.app_label
    524524
    525525        try:
    526             obj = model._default_manager.get(pk=object_id)
     526            obj = self.queryset(request).get(pk=object_id)
    527527        except model.DoesNotExist:
    528528            # Don't raise Http404 just yet, because we haven't checked
    529529            # permissions yet. We don't want an unauthenticated user to be able
    class ModelAdmin(BaseModelAdmin):  
    621621        app_label = opts.app_label
    622622
    623623        try:
    624             obj = self.model._default_manager.get(pk=object_id)
     624            obj = self.queryset(request).get(pk=object_id)
    625625        except self.model.DoesNotExist:
    626626            # Don't raise Http404 just yet, because we haven't checked
    627627            # 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 2062107..b533f00 100644
    a b class CustomArticle(models.Model):  
    3232    content = models.TextField()
    3333    date = models.DateTimeField()
    3434
     35class EmptyModel(models.Model):
     36    def __unicode__(self):
     37        return "Primary key = %s" % self.id
     38
     39class EmptyModelAdmin(admin.ModelAdmin):
     40    def queryset(self, request):
     41        return super(EmptyModelAdmin, self).queryset(request).filter(pk__gt=1)
     42
    3543class CustomArticleAdmin(admin.ModelAdmin):
    3644    """
    3745    Tests various hooks for using custom templates and contexts.
    class ModelWithStringPrimaryKey(models.Model):  
    5866admin.site.register(Article, ArticleAdmin)
    5967admin.site.register(CustomArticle, CustomArticleAdmin)
    6068admin.site.register(Section)
     69admin.site.register(EmptyModel, EmptyModelAdmin)
    6170admin.site.register(ModelWithStringPrimaryKey)
  • tests/regressiontests/admin_views/tests.py

    diff --git a/tests/regressiontests/admin_views/tests.py b/tests/regressiontests/admin_views/tests.py
    index ad50928..ed9447a 100644
    a b from django.contrib.admin.util import quote  
    88from django.utils.html import escape
    99
    1010# local test models
    11 from models import Article, CustomArticle, Section, ModelWithStringPrimaryKey
     11from models import Article, CustomArticle, Section, EmptyModel, ModelWithStringPrimaryKey
    1212
    1313def get_perm(Model, perm):
    1414    """Return the permission object, for the Model"""
    class AdminViewStringPrimaryKeyTest(TestCase):  
    360360        response = self.client.get('/test_admin/admin/admin_views/modelwithstringprimarykey/%s/delete/' % quote(self.pk))
    361361        should_contain = """<a href="../../%s/">%s</a>""" % (quote(self.pk), escape(self.pk))
    362362        self.assertContains(response, should_contain)
     363
     364class AdminCustomQuerytetTest(TestCase):
     365    fixtures = ['admin-views-users.xml']
     366   
     367    def setUp(self):
     368        self.client.login(username='super', password='secret')
     369        self.pks = []
     370        self.pks.append(EmptyModel.objects.create().id)
     371        self.pks.append(EmptyModel.objects.create().id)
     372        self.pks.append(EmptyModel.objects.create().id)
     373   
     374    def test_changelist_view(self):
     375        response = self.client.get('/test_admin/admin/admin_views/emptymodel/')
     376        for i in self.pks:
     377            if i > 1:
     378                self.assertContains(response, 'Primary key = %s' % i)
     379            else:
     380                self.assertNotContains(response, 'Primary key = %s' % i)
     381   
     382    def test_change_view(self):
     383        for i in self.pks:
     384            response = self.client.get('/test_admin/admin/admin_views/emptymodel/%s/' % i)
     385            if i > 1:
     386                self.assertEqual(response.status_code, 200)
     387            else:
     388                self.assertEqual(response.status_code, 404)
     389               
     390               
     391               
Back to Top