Django Admin - Custom Change List View

I need to add a custom view in Django Admin. This should be similar to the standard ChangeList view for a specific model, but with a custom result set. (I need to display all models that have a certain date or , some other date is less than today, but this is not relevant).

One way to do this is to use the Admin method queryset, for example

class CustomAdmin(admin.ModelAdmin):
    ...
    def queryset(self, request):
        qs = super(CustomAdmin, self).queryset(request)
        if request.path == 'some-url':
            today = date.today()
            # Return a custom queryset
        else:
            return qs

This ensures that ...

The problem is that I don’t know how to associate some-urlwith the standard ChangeList view.

+5
source share
1 answer

, , URL- , , URL-, . , django.contrib.admin.options, URL- ModelAdmin.

:

class CustomAdmin(admin.ModelAdmin):

    def get_urls(self):
        def wrap(view):
            def wrapper(*args, **kwargs):
                kwargs['admin'] = self   # Optional: You may want to do this to make the model admin instance available to the view
                return self.admin_site.admin_view(view)(*args, **kwargs)
            return update_wrapper(wrapper, view)

        # Optional: only used to construct name - see below
        info = self.model._meta.app_label, self.model._meta.module_name

        urlpatterns = patterns('',
            url(r'^my_changelist/$',   # to your liking
                wrap(self.changelist_view),
                name='%s_%s_my_changelist' % info)
        )
        urlpatterns += super(CustomAdmin, self).get_urls()
        return urlpatterns
+5

All Articles