[Django]-How to Change order of Modules listed in Side Bar of Django Admin

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.

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

Leave a comment