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. Generate and render HTML Table by LLyaudet 5 days, 15 hours ago
  2. My firs Snippets by GutemaG 1 week, 1 day ago
  3. FileField having auto upload_to path by junaidmgithub 1 month, 2 weeks ago
  4. LazyPrimaryKeyRelatedField by LLyaudet 1 month, 3 weeks ago
  5. CacheInDictManager by LLyaudet 1 month, 3 weeks ago

Comments

Please login first before commenting.