183👍
class SeminarInline(admin.StackedInline):
model = Seminar
extra = 0
ordering = ('-date',)
worked for me (above adapted from my model)
It sorted in descending date order
25👍
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
- [Django]-How to produce a 303 Http Response in Django?
- [Django]-Add custom form fields that are not part of the model (Django)
- [Django]-Inline in ModelForm
24👍
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:
- [Django]-Django Rest Framework with ChoiceField
- [Django]-Add a custom button to a Django application's admin page
- [Django]-Profiling Django
23👍
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.
- [Django]-Django queries: how to filter objects to exclude id which is in a list?
- [Django]-No module named pkg_resources
- [Django]-Querying django migrations table
9👍
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
- [Django]-AbstractUser vs AbstractBaseUser in Django?
- [Django]-How do I reference a Django settings variable in my models.py?
- [Django]-Using Cloudfront with Django S3Boto
7👍
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!
- [Django]-Django template can't see CSS files
- [Django]-Check if an object exists
- [Django]-Get user profile in django
0👍
Yes, you can add ordering to your ModelAdmin. This is separated from your models.py ordering.
- [Django]-Unique BooleanField value in Django?
- [Django]-Django-taggit – how do I display the tags related to each record
- [Django]-On Heroku, is there danger in a Django syncdb / South migrate after the instance has already restarted with changed model code?