Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ordering the display items alphabetically in Django-Admin

I am now able to display the item I want using the list_display[] but I would like them to displayed alphabetically. How do you make that the list_display is ordered alphabetically?

Example in Django-admin:

class PersonAdmin(admin.ModelAdmin):
    list_display = ('upper_case_name',)

    def upper_case_name(self, obj):
      return ("%s %s" % (obj.first_name, obj.last_name)).upper()
    upper_case_name.short_description = 'Name'

How to alter this to display alphabetically? Need some help on this...

like image 368
lakshmen Avatar asked Aug 17 '12 04:08

lakshmen


2 Answers

Set ModelAdmin.ordering = ('foo', ) to order your admin.

Example

class TeamRoleAdmin(admin.ModelAdmin):
   """ Team Role admin view with modifications """
   model = TeamRole
   ordering = ('team', 'role', 'user')
   list_display = ['id', 'user', 'team', 'role']

https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.ordering

Modify global model meta class, override ModelAdmin.queryset, there are many ways but you most likely want the Admin ordering.

like image 161
Yuji 'Tomita' Tomita Avatar answered Nov 19 '22 17:11

Yuji 'Tomita' Tomita


In your models Meta you can specify ordering = ['first_name', 'last_name'].

like image 4
Rohan Avatar answered Nov 19 '22 17:11

Rohan