Django adding custom view in admin site

djangodjango-admin

I am using Django 1.4. I would like to add a queue in admin page which should look like admin change_list page. I tried so many blogs and other forum, but did not get what I want. How can I add custom view in admin page with following features,

  • My own queryset
  • should look like admin changelist page with all features available there.

models.py

class Broker(models.Model):
    user = models.OneToOneField(User)
    agency = models.CharField(max_length=100)

urls.py

    url(r'^admin/broker_list/$', 'room.admin_views.broker_list'),
    url(r'^admin/', include(admin.site.urls)), 

admin_views.py

def broker_list(request):
    return render_to_response(
        "admin/broker_list.html",
        {'broker_list' : Broker.objects.filter(user__is_active=False)},
        RequestContext(request, {}),
    )
broker_list = staff_member_required(broker_list)

broker_list.html

{% extends "admin/base_site.html" %}
{% load admin_urls %}

{% block title %}List of pending agent{% endblock %}

{% block content %}
    {{broker_list}}
{% endblock %}

With above said code, I can able to go access the link /admin/broker_list/ where I can see the queryset. But I do not know how to generate or extend like admin chagelist page.

Even I tired with Django ModelAdmin get_urls method, but the document stated that my_view should return HttpResponse, so it renders only raw object. for the reference I shared my code below,

admin.py

class ItemAdmin(admin.ModelAdmin):
    model = Broker
    def admin_list_broker(self, request):
        print "yes iam here"
        return HttpResponse("Hello!")
        #return HttpResponseRedirect(
        #        reverse("admin:account_agent_changelist",)
        #)
    def get_urls(self):
        from django.conf.urls.defaults import *
        urls = super(ItemAdmin, self).get_urls()
        my_urls = patterns('',
            url(
                r'broker_list',
                self.admin_site.admin_view(self.admin_list_broker),
                name='admin_list_broker',
            ),
        )
        print my_urls + urls
        return my_urls + urls

admin.site.register(Broker, ItemAdmin)

Please help me to implement the requirement in anyone one above said method.

Thanks.

Best Answer

Rather then creating a custom admin change view I would approach this problem by extending the built in one, the django admin provides a number of hooks to facilitate this, here are my thoughts:

  1. Modify the default queryset but I would recommend against this as you loose the ability to edit objects not returned by the queryset.
  2. Create a custom filter that would display your 'broker list' queryset - I recommend this one as it further will give you a GET url query you can link to directly to activate this filter.
  3. You can even completely gut out the change view and use your own - this option I have the least experience with and can not comment.

You may wish to also take at look changing the change list template and admin custom actions to further customize the look and feel and to provide custom 'actions'; I have used both successfully in the past to provide project specific functionality.

Very exciting are the has_add_premission, had_change_permission, and has_delete_permission hooks, couple these with like django-guradian and custom admin base template could allow you to use the backend admin as a complete front end administration.

Do take the time to throughly read the entire model admin page - the better I know it the less I find myself coding custom front end administration.

Related Topic