This is a simple snippet of code for reordering your django admin-menu leveraging and rewriting Django Admin's get_app_list function.
- Copy the code from
django-reorder-admin-menu.pyand place it on the end of yoursettings.pyas below:
def get_app_list(self, request, app_label=None):
[copied code]
# Covering django.contrib.admin.AdminSite.get_app_list
from django.contrib import admin
admin.AdminSite.get_app_list = get_app_list- Just above this, create a list called
ADMIN_ORDERING, this is where you will configure the ordering and menus for your Django Admin
ADMIN_ORDERING = []ADMIN_ORDERING is a list of dicts, each dict must contain an app definition, and optionally a label and models definition
appis the name of the main django app this entry is defininglabelis the name you want the menu entry to have, if not provided theapp's verbose_name will be usedmodelsis the, ordered, list of models you want this menu to have, if not provided, all registered models from theappwill be added
For convenience, two general operators are supported:
*if you want to add all registered models from theappto the menu%if you want to add all registered models that haven't been added in other menu entries yet
- Any model added that isn't registered will be ignored
- For now, to avoid extra loops, the
%operator doesn't look ahead, so if you add it before declaring all the app's models you want, it will duplicate your entries. Use it only after declaring all models - Breadcrumbs aren't altered, but they are supported: if you click a breadcrumb for an app you didn't define in
ADMIN_ORDERING, it will still work, displaying all models for that app.
You can add models from other apps using dot notation.
For example, if you have an accounts app with an User model and want to add it to the auth menu, you can do as follows:
ADMIN_ORDERING = [
{"app": "auth", "models": ["Group", "accounts.User"]}
]ADMIN_ORDERING = [
{"app": "auth", "models": ["Group", "accounts.User"]},
{"app": "app1", "label": "My custom label", "models": ["*"]},
{
"app": "app2",
"models": ["Model1", "Model2"],
},
{
"app": "app2",
"label": "app2:fun_stuff",
"models": [
"Model3",
"Model4",
"Model5",
"Model6",
],
},
{
"app": "app2",
"label": "app2 - other stuff",
"models": ["%"],
},
]Will result in something like this
This code is heavily based on mishbahr's django-modeladmin-reorder and on a plethora of StackOverflow's answers whose references have been lost in the mess that was coding this.
If you believe your code is in any way refereced here, please open an issue or PR.
