Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defining a custom app_list in django admin index page

I'd like to define a custom application list to use in django's admin index page because I want the apps displayed in a specific order, rather than the default alphabetical order. Trawling through various SO posts it would appear that it's not yet possible to declare the desired application order in any of the obvious places (e.g. admin.py, models.py).

Now, I can see that the django admin's index.html file contains the following statement:

{% for app in app_list %}
   # do stuff with the app object

So I'd like to change this to use a custom list object called, say, my_app_list. In python I'd do this along the following lines:

from django.db.models import get_app
my_app_list = [get_app('myapp1'), get_app('myapp2'), ..., get_app('django.contrib.auth')]
for app in my_app_list
   ...

My question then is, how do I code the equivalent of the first 2 lines above into my local copy of the index.html file?

Or, alternatively, what python source file should I insert those lines into such that the variable my_app_list is available within index.html.

Thanks in advance.

Phil

like image 466
Phil Avatar asked Jan 17 '12 11:01

Phil


1 Answers

Subclass django.contrib.admin.site.AdminSite(). Override the .index() method, and do something like this:

class MyAdminSite(django.contrib.admin.site.AdminSite):
    def index(self, request, extra_context=None):
        if extra_context is None:
            extra_context = {}
        extra_context["app_list"] = get_app_list_in_custom_order()
        return super(MyAdminSite, self).index(request, extra_context)

Instantiate an instance of this subclass with my_admin_site = MyAdminSite(), attach your models to it (using the usual my_admin_site.register()), and attach it to the URLconf; that should do it.

(I haven't tried this, I'm basing this on my reading of the AdminSite source.)

like image 124
AdamKG Avatar answered Oct 02 '22 14:10

AdamKG