Django admin ordering models - easy / right way

I want to order models in my Django admin interface. By default, the alphabetical order of models in applications is used.

I would like to avoid creating additional packages, etc. etc. or change Django itself.

Can this be done by overriding the Django admin templates or some other easy way?

EDIT:

I do not want to order finite elements, such as a list of tags.

I want to order various models on the admin index page. For example. todos come in front of cars .

+7
source share
5 answers

You can override the template and add a template template for sorting. Here's a snippet, some change may be required http://djangosnippets.org/snippets/1939/

+4
source

I don’t think there is an easy way to do this - on the one hand, there is no way to store an order, so it will at least include a new database field, and then the code that receives this field to find the required ordering. Actually, this may not work, because AdminSite is created dynamically and is not stored in the database ... Hmm, let's see what Django does ...

An index template is just a cycle over applications registered by the administrator, and in each application there is a cycle over models. Their order is determined by lines 384 and further in https://github.com/django/django/blob/master/django/contrib/admin/sites.py - so that one hook will subclass AdminSite and rewrite the index method. Here are the new AdminSite stuff discussed: https://docs.djangoproject.com/en/dev/ref/contrib/admin/#adminsite-objects

Perhaps if you want to present the models in the order in which they are registered, you can simply take out the sort lines in this method.

+3
source

I leave a not very big answer.

Models are sorted on line 448 in django / contrib / admin / sites.py

448 # Sort the models alphabetically within each app. 449 for app in app_list: 450 app['models'].sort(key=lambda x: x['name']) 

I have not tested this, but you can try adding a variable to the Meta class that you would use to set the order of the models on the admin index page in a for loop on line 403.

 403 for model, model_admin in self._registry.items(): ,,, 431 else: 432 order = model._meta.order 433 app_dict[app_label] = { 434 'order': order, 435 ... 

And replace the sort as follows:

 450 app['models'].sort(key=lambda x: x['order']) 
+2
source

I just did it with proxy models so that the models can be grouped under one heading. I realized that you can adjust the sort order by simply changing the verbose_name and verbose_name_plural proxy models. That way, the top-level name can be reached using "+ ._ meta.verbose_name, while others can simply be reordered to 1., 2. 3., ...

I am sure that this is not what you are looking for, but it was as easy as I could achieve with acceptable results.

 # foo/admin.py from django.contrib import admin from zoo.models import Zoo class ZooProxy(Zoo): class Meta: proxy = True app_label = 'foo' verbose_name = " " + Zoo._meta.verbose_name verbose_name_plural = " " + Zoo._meta.verbose_name_plural # one/admin.py from django.contrib import admin from one.models import One class OneProxy(One): class Meta: proxy = True app_label = 'foo' verbose_name = "2. " + One._meta.verbose_name verbose_name_plural = "2. " + One._meta.verbose_name_plural # two/admin.py from django.contrib import admin from two.models import Two class TwoProxy(Two): class Meta: proxy = True app_label = 'foo' verbose_name = "1. " + Two._meta.verbose_name verbose_name_plural = "1. " + Two._meta.verbose_name_plural 

run the django admin command:

 Foo: Zoo 1. Two 2. One -vs- Foo: One Two Zoo 
+2
source

I'm not sure that Django provides default sorting, however you can use ModelAdmin objects to add a filtering and sorting panel in the Django admin. It looks something like this. enter image description here

These changes must be made in the admin.py file.

 class PersonAdmin(ModelAdmin): list_filter = ('is_staff', 'company') 

list_filter will add this panel that can serve your purpose of sorting and filtering

See below for more details: https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_filter

-3
source

All Articles