Login

Admin Model Sorting

Author:
programmerDan
Posted:
August 8, 2011
Language:
Python
Version:
1.2
Score:
1 (after 1 ratings)

This allows you to order the models on the index page of the administration site in a custom way.

This modification goes in the index method of django.contrib.admin.sites.AdminSite. I personally monkey patched it in where my models are loaded and registered with the admin site. Be careful that if you add new models you update the sorting dictionary, else you will get a key error. If no sorting is defined for an app, it will default to alphabetical order.

Note that you'll probably want to also insert this into the app_index function as well.


If you like my work, please check out my employer's site at 829llc.com - Dan

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# Each key in this dictionary corresponds to our application's url, including trailing slash.
# The value of each entry is a list of URLs of our models, in the order we would like them to be displayed.
modelOrder = {}
modelOrder['agentAccess/'] = ['agentAccess/post/', 'agentAccess/post_category/', 'agentAccess/page/', 'agentAccess/page_category/', 'agentAccess/fileupload/']

# Check if we have a corresponding ordering list, if not, sort by name.
for app in app_list:
    if modelOrder.get(app['app_url'], None):
        app['models'].sort(lambda x, y: cmp(modelOrder[app['app_url']].index(x['admin_url']), modelOrder[app['app_url']].index(y['admin_url'])))
    else:
        app['models'].sort(lambda x, y: cmp(x['name'], y['name']))

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 10 months, 1 week ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 6 months ago

Comments

Please login first before commenting.