Opened 8 months ago
Closed 8 months ago
#35302 closed New feature (wontfix)
Property on AppConfig and ModelAdmin for ordering apps and models in admin sidebar
Reported by: | Michał Pokusa | Owned by: | Michał Pokusa |
---|---|---|---|
Component: | contrib.admin | Version: | dev |
Severity: | Normal | Keywords: | admin models apps ordering reorder sidebar move |
Cc: | Michał Pokusa | Triage Stage: | Unreviewed |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | yes | UI/UX: | no |
Description
Currently apps and models registered are sorted only based on "name" that comes from AdminSite._build_app_dict
.
There is no easy way of reordering models and/or app in the sidebar. Solutions found online suggest overriding AdminSite.get_app_list
, but even this approach is not perfect as it uses the dict
from AdminSite._build_app_dict
, which contains limited information about apps and its models. I remember even finding a solution that used JavaScript for manual reordering of DOM elements.
Seems like there are many people, including me, that want to reorder the admin sidebar, I assume for more logical placement of models.
Example where this makes sense is an app with "Question" and "Answer" models, where "Answers" will be sorted before "Questions", despite the opposite making more sense. Another example is moving main business logic apps to the top, where they are easily accessible and moving less often used apps to the bottom.
I propose adding a new property, e.g. "sidebar_order" (0
by default) or something like this, that could be set on AppConfig
and ModelAdmin
and would have priority when determinining the order in admin sidebar and/or model listing when accesing specific app's admin. If it is not provided, the standard ordering based on name would take effect. I believe this would fit with current similar Django options, like ordering
, list_display
, custom order of filters etc.
Example:
@admin.register(Question) class QuestionAdmin(admin.ModelAdmin): sidebar_order = 1 ... @admin.register(Answer) class AnswerAdmin(admin.ModelAdmin): sidebar_order = 2 ...
My proposed solution would be 100% backwards compatible. This change requires literally change in 4 lines in django.contrib.admin.sites
for it to work (that is why i mark it as Easy picking), of course more for proper implementation.
Also it would be easy to add ordering to new models without changing the ordering of all other models, as one could set the property to values like 10
, 20
instead of 1
, 2
, which would allow setting new models ordering to any value between e.g. 15
.
Thanks for this ticket, however in #7497 we decided that in most of cases
get_app_list()
is enough to change the default order and we're not going to add any new options around that.