Django admin sort order

You can add Meta options to a Django model which can dictate how it behaves. There is an ordering option which defines by which model attribute records should be ordered.

You can find the documentation for the meta ordering option here in the Django docs:


Below is the method as per 4.0 documentation

# mymodel/admin.py

from django.contrib import admin
from . import models

# admin.site.register(models.MyModel)

@admin.register(models.MyModel)
class MyModelAdmin(admin.ModelAdmin):
    ordering = ['-last_name']

Here last_name is the field inside MyModel.


There is also the possibility to override get_ordering(self, request) of the ModelAdmin which allows for case insensitive ordering:

from django.db.models.functions import Lower

class MyModelAdmin(ModelAdmin):
    list_display = ('name',)
    search_fields = ['name']

    def get_ordering(self, request):
        return [Lower('name')]  # sort case insensitive

class SeminarInline(admin.StackedInline):
    model = Seminar
    extra = 0
    ordering = ('-date',)

worked for me (above adapted from my model) It sorted in descending date order