lakshmen
lakshmen

Reputation: 29064

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...

Upvotes: 15

Views: 22593

Answers (2)

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.

Upvotes: 28

Rohan
Rohan

Reputation: 53326

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

Upvotes: 4

Related Questions