Django admin sort order
class SeminarInline(admin.StackedInline):
model = Seminar
extra = 0
ordering = ('-date',)
worked for me (above adapted from my model) It sorted in descending date 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:
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
If you want to define a order within an InlineAdmin django doesn't offer you a a generic solution to do this! There are some snippets out there that enable you to add this functionality to the admin, also the grappelli skin offers you such a feature!
For example if you want the table to be sorted by percentage :
- Go to models.py file in your mainapp
-
class Meta: abstract = True ordering = ['-percentage'] #Sort in desc order
-
class Meta: abstract = True ordering = ['percentage'] #Sort in asc order