Custom Ordering and Grouping of Models in Django Admin

73 views
Skip to first unread message

Vasanth Mohan

unread,
Sep 16, 2021, 1:45:59 PMSep 16
to Django developers (Contributions to Django itself)
As the numbers Models in an app grows, it becomes tedious to find the model we are searching for in a long list on the index page (and navigation bar) of the Django admin

As a part of a new project, I wanted to reorder and logically group models that do not necessarily reflect the apps of the project. I found this little library called [django-modeladmin-reorder](https://github.com/mishbahr/django-modeladmin-reorder) that does it by remapping the fields with a middleware.

However, I think it would be cleaner if Django supported it natively as this can be useful in almost every project. A layer called AdminApp between the `admin` and the ModelAdmin registered would be the perfect middle ground.

**Proposal**

- Create a new class in admin called `AdminApp`
    - The AdminApp should be registerable to admin
    - The AdminApp should accept ModelAdmin to be registered with it
- Wire up the ModelAdmins and AdminApps to see custom layout that is not tied to apps
- For all ModelAdmins registered directly to admin, fallback to current implementation

```
class AgencyAdmin(admin.ModelAdmin):
   pass

class AgencyConfigAdmin(admin.ModelAdmin):
   pass

class AgencyAdminApp(admin.AdminApp):
   label = "Agency Details"

class AgentAdmin(admin.ModelAdmin):
   pass


admin.register(AgentAdmin) # Existing implementation

# Option 1 : without decorators
## Add Models to AdminApp
AgencyAdminApp.register(AgencyAdmin)
AgencyAdminApp.register(AgencyConfigAdmin)
## Add New AdminApp tp Admin
admin.register_app(AgencyAdminApp)

# Option 2 : with decorators
admin.register(AgencyAdmin, admin_app=AgencyAdminApp)
admin.register(AgencyConfigAdmin, admin_app=AgencyAdminApp)
```

I feel like this seems like a decent middle ground that doesn't break existing implementation but allows the app to be expanded in the future.

So what do you think about the idea? If the core team is interested, I'll be more than willing to work on this but I am first time contributor and will need some mentoring on it.

 I've also made a forum post for the same : https://forum.djangoproject.com/t/custom-admin-apps-for-grouping-and-reordering-models/9723

Mariusz Felisiak

unread,
Sep 16, 2021, 2:39:33 PMSep 16
to Django developers (Contributions to Django itself)
Hi,

    Thanks for the proposition. It was discussed few times in the past. AdminSite.get_app_list() could be used to re-arrange models and apps, see ticket-5986 and its duplicates ticket-25671 and ticket-32482. Feel-free to prepare a patch.

Best,
Mariusz
Reply all
Reply to author
Forward
0 new messages