The display order of display elements in alphabetical order in Django-Admin

Now I can display the item I want using list_display [], but I would like them to be displayed in alphabetical order. How do you make list_display sorted 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 change this to display in alphabetical order? Need help with this ...

+6
source share
2 answers

Set ModelAdmin.ordering = ('foo', ) to order an administrator.

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

Change the global meta class of the model, override ModelAdmin.queryset, there are many ways, but you most likely want to get an administrator order.

+12
source

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

+1
source

Source: https://habr.com/ru/post/923093/


All Articles