[Fixed]-Reorder model objects in django admin panel

4đź‘Ť

âś…

I don’t see an obvious solution to this — the models are sorted by their _meta.verbose_name_plural, and this happens inside the AdminSite.index view, with no obvious place to hook custom code, short of subclassing the AdminSite class and providing your own index method, which is however a huge monolithic method, very inheritance-unfriendly.

👤lanzz

12đź‘Ť

So I’ve just wrote a django package that will allow you reorder django app list/ rename app label and model names (including 3rd party apps) — you can download it from here:

https://github.com/mishbahr/django-modeladmin-reorder

👤mishbah

6đź‘Ť

If you don’t mind using a dirty trick, prepend verbose_name_plural of your models with a certain number of invisible zero-width spaces. E.g. prepend “Email config” with 1 zero-width space, “General config” with 2 and “Network config” with 3. This is really the simplest method and I’ve yet to find any shortcomings.

👤Dae

1đź‘Ť

I found this snippet for reordering models in the django admin panel – it works for us (though check out the comments below the snippet for updates to make it work with Django >= 1.4)

And for the longer term there is this django bug report about the ordering of models within apps. The bug report is open at the time of writing this answer.

👤Hamish Downer

0đź‘Ť

If you just want to reorder your apps (tested with Django 1.11.10).

class MyAdminSite(AdminSite):
    def index(self, request, extra_context=None):
        if extra_context is None:
            extra_context = {}

        # Move last app to the top of `app_list`.
        app_list = self.get_app_list(request)
        app_list.insert(0, app_list.pop(-1))

        extra_context['app_list'] = app_list
        return super().index(request, extra_context)

Leave a comment