6
Two steps necessary to override the default ordering for your admin site.
First would be subclassing Django’s built in AdminSite. This will allow you to customize your app list as you see fit.
Specifically you will want to look to override the get_app_list method for your AdminSite subclass.
per Django Docs on AdminSite.get_app_list
method:
Lists of applications and models are sorted alphabetically by their names. You can override this method to change the default order on the admin index page.
2
There is is a pip package for it django-reorder-admin.
There is a full guide on how to use it in the documentation.
- [Django]-Django admin with websocket
- [Django]-Django – Dynamically importing a models form
- [Django]-Make celery wait for task to finish
- [Django]-How can this be written on a single line?
0
Since I had the exact same problem, and to complete Alexi’s answer, here is an implementation of a get_app_list
method that just swaps two django admin models.
This is quite hackish, but does the job.
class CustomAdminSite(admin.AdminSite):
def get_app_list(self, request, app_label=None):
"""Reorder the apps in the admin site.
By default, django admin apps are order alphabetically.
To be more consistent with the actual worflow, we want the "Demande d'avis"
app listed before the "Avis" one.
And since django does not offer a simple way to order app, we have to tinker
with the default app list, find the indexes of the two apps in the list,
and swap them.
"""
apps = super().get_app_list(request, app_label)
# Find the index of the "evaluations" app in the list of top level apps
evaluations = next(
(
index
for (index, app) in enumerate(apps)
if app["app_label"] == "evaluations"
),
None,
)
# Find the indexes of the "Avis" and "Demande d'avis" models in the "evaluations" app
avis_index = next(
(
index
for (index, app) in enumerate(apps[evaluations]["models"])
if app["object_name"] == "Evaluation"
),
None,
)
demande_index = next(
(
index
for (index, app) in enumerate(apps[evaluations]["models"])
if app["object_name"] == "Request"
),
None,
)
# And do the swap, python style
(
apps[evaluations]["models"][avis_index],
apps[evaluations]["models"][demande_index],
) = (
apps[evaluations]["models"][demande_index],
apps[evaluations]["models"][avis_index],
)
return apps
- [Django]-Django Model API reverse lookup of many to many relationship through intermediary table
- [Django]-Could not find a version that satisfies the requirement pywin32==227 heroku
- [Django]-Custom settings and wsgi in django 1.10 give me error
- [Django]-Django Gunicorn: Unable to find application