[Django] #35302: Property on AppConfig and ModelAdmin for ordering apps and models in admin sidebar

8 views
Skip to first unread message

Django

unread,
Mar 14, 2024, 10:16:22 PM3/14/24
to django-...@googlegroups.com
#35302: Property on AppConfig and ModelAdmin for ordering apps and models in admin
sidebar
-------------------------------------+-------------------------------------
Reporter: Michał | Owner: Michał Pokusa
Pokusa |
Type: New | Status: assigned
feature |
Component: | Version: dev
contrib.admin | Keywords: admin models apps
Severity: Normal | ordering reorder sidebar move
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 1
UI/UX: 0 |
-------------------------------------+-------------------------------------
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`.
--
Ticket URL: <https://code.djangoproject.com/ticket/35302>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Mar 15, 2024, 1:31:13 AM3/15/24
to django-...@googlegroups.com
#35302: Property on AppConfig and ModelAdmin for ordering apps and models in admin
sidebar
-------------------------------------+-------------------------------------
Reporter: Michał Pokusa | Owner: Michał
| Pokusa
Type: New feature | Status: closed
Component: contrib.admin | Version: dev
Severity: Normal | Resolution: wontfix
Keywords: admin models apps | Triage Stage:
ordering reorder sidebar move | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* resolution: => wontfix
* status: assigned => closed

Comment:

Thanks for this ticket, however in #7497 we
[https://code.djangoproject.com/ticket/25671#comment:11 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.
--
Ticket URL: <https://code.djangoproject.com/ticket/35302#comment:1>
Reply all
Reply to author
Forward
0 new messages