Login

Ordering Models in Django Site administration screen

Author:
btbytes
Posted:
August 8, 2007
Language:
Python
Version:
.96
Tags:
admin
Score:
0 (after 0 ratings)

The models in the Site Administration screen are listed alphabetically. If you need to order them differently, say, in the order of creation of objects or some other 'workflow' criterion, you can use the instructions in this snippet.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
'''Copy django/contrib/admin/templatetags/adminapplist.py 
to the myapp/templatetags/ directory. say my_adminapplist.py 
'''

#my_adminapplist.py 

# under the block 
if True in perms.values():
    import random
    admin_order = random.randint(100,999)
    if hasattr(m, '_order'):
        admin_order = m._order
        model_list.append({
            'name': force_unicode(capfirst(m._meta.verbose_name_plural)),
            'admin_url': u'%s/%s/' % (force_unicode(app_label), m.__name__.lower()),
            'perms': perms,
            'admin_order':admin_order,
        })

# and then under model_list block:
if model_list:
    decorated = [(x['admin_order'], x) for x in model_list]
# this will sort the code based on admin_order key instead of alphabetical order

''' Most importantly, add a line `_order` in your model file for each Model'''

_order = 1 # this is the order in which the models will be ordered.

More like this

  1. Admin Model Sorting by programmerDan 3 years, 9 months ago
  2. Get admin url for a model by timbroder 5 years, 3 months ago
  3. models with order (+admin editing) by bram 6 years, 9 months ago
  4. Redirect with change list with filters intact with admin actions by AndrewIngram 5 years, 10 months ago
  5. SortableModel - abstract model class for sortable records by bendavis78 5 years, 11 months ago

Comments

Please login first before commenting.