Ticket #9749: 9749-different-approach.diff

File 9749-different-approach.diff, 4.5 KB (added by floguy, 5 years ago)

My version

  • django/contrib/admin/options.py

     
    885885        context.update(extra_context or {})
    886886        return self.render_change_form(request, context, change=True, obj=obj)
    887887
     888    def get_changelist(self, request, **kwargs):
     889        from django.contrib.admin.views.main import ChangeList
     890        return ChangeList
     891
    888892    @csrf_protect
    889893    def changelist_view(self, request, extra_context=None):
    890894        "The 'change list' admin view for this model."
    891         from django.contrib.admin.views.main import ChangeList, ERROR_FLAG
     895        from django.contrib.admin.views.main import ERROR_FLAG
    892896        opts = self.model._meta
    893897        app_label = opts.app_label
    894898        if not self.has_change_permission(request, None):
     
    905909            except ValueError:
    906910                pass
    907911
     912        changelist = self.get_changelist(request)
    908913        try:
    909             cl = ChangeList(request, self.model, list_display, self.list_display_links, self.list_filter,
     914            cl = changelist(request, self.model, list_display, self.list_display_links, self.list_filter,
    910915                self.date_hierarchy, self.search_fields, self.list_select_related, self.list_per_page, self.list_editable, self)
    911916        except IncorrectLookupParameters:
    912917            # Wacky lookup parameters were given, so redirect to the main
  • tests/regressiontests/admin_views/tests.py

     
    12031203        self.failUnlessEqual(Subscriber.objects.count(), 2)
    12041204
    12051205
     1206class TestCustomChangeList(TestCase):
     1207    fixtures = ['admin-views-users.xml']
     1208   
     1209    urlbit = 'admin'
     1210   
     1211    def setUp(self):
     1212        result = self.client.login(username='super', password='secret')
     1213        self.failUnlessEqual(result, True)
     1214
     1215    def tearDown(self):
     1216        self.client.logout()
     1217
     1218    def test_custom_changelist(self):
     1219        """
     1220        Validate that a custom ChangeList class can be used (#9749)
     1221        """
     1222        # Insert some data
     1223        post_data = {"name": u"First Gadget"}
     1224        response = self.client.post('/test_admin/%s/admin_views/gadget/add/' % self.urlbit, post_data)
     1225        self.failUnlessEqual(response.status_code, 302) # redirect somewhere
     1226        # Hit the page once to get messages out of the queue message list
     1227        response = self.client.get('/test_admin/%s/admin_views/gadget/' % self.urlbit)
     1228        # Ensure that that data is still not visible on the page
     1229        response = self.client.get('/test_admin/%s/admin_views/gadget/' % self.urlbit)
     1230        self.failUnlessEqual(response.status_code, 200)
     1231        self.assertNotContains(response, 'First Gadget')
     1232
     1233
    12061234class TestInlineNotEditable(TestCase):
    12071235    fixtures = ['admin-views-users.xml']
    12081236
  • tests/regressiontests/admin_views/models.py

     
    44from django.core.files.storage import FileSystemStorage
    55from django.db import models
    66from django.contrib import admin
     7from django.contrib.admin.views.main import ChangeList
    78from django.core.mail import EmailMessage
    89
    910class Section(models.Model):
     
    420421class CollectorAdmin(admin.ModelAdmin):
    421422    inlines = [WidgetInline, DooHickeyInline, GrommetInline, WhatsitInline, FancyDoodadInline, CategoryInline]
    422423
     424class Gadget(models.Model):
     425    name = models.CharField(max_length=100)
     426   
     427    def __unicode__(self):
     428        return self.name
     429
     430class CustomChangeList(ChangeList):
     431    def get_query_set(self):
     432        return self.root_query_set.filter(pk=9999) # Does not exist
     433
     434class GadgetAdmin(admin.ModelAdmin):
     435    def get_changelist(self, request, **kwargs):
     436        return CustomChangeList
     437
    423438admin.site.register(Article, ArticleAdmin)
    424439admin.site.register(CustomArticle, CustomArticleAdmin)
    425440admin.site.register(Section, save_as=True, inlines=[ArticleInline])
     
    443458admin.site.register(Recommender)
    444459admin.site.register(Collector, CollectorAdmin)
    445460admin.site.register(Category, CategoryAdmin)
     461admin.site.register(Gadget, GadgetAdmin)
    446462
    447463# We intentionally register Promo and ChapterXtra1 but not Chapter nor ChapterXtra2.
    448464# That way we cover all four cases:
Back to Top