Login

Admin Model Sorting

Author:
programmerDan
Posted:
August 8, 2011
Language:
Python
Version:
1.2
Tags:
models admin sort sorting app-models
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. Django Admin Index Custom App & Model Ordering by hsc 3 years, 5 months ago
  2. reorder apps in admin index by reanes 5 years, 6 months ago
  3. Admin: ordering by multiple fields in a column sort by benatkin 4 years, 10 months ago
  4. Mini issue tracker by pbx 8 years, 2 months ago
  5. Ordering Models in Django Site administration screen by btbytes 7 years, 9 months ago

Comments

Please login first before commenting.