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
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.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With