1Up
1Up

Reputation: 1054

How to group models in django admin?

Lets say I have a group of models that must be kept separate (visually) in the admin from another group.

Right now they are alphabetic, which jumbles them.

I'd like to organize them this way:


Group 1: (custom named)

Group 2 (custom named)


I cannot seem to find the documentation on how to do this. Is this even possible?

Upvotes: 26

Views: 18538

Answers (3)

Ariful Haque
Ariful Haque

Reputation: 163

Please make sure that your model names in ADMIN_REORDER have to be exact as written in your models.py file.

For me, it worked with Django==3.1.5 without any issue. Here is my sample code in the settings.py file

ADMIN_REORDER = (
# 'webapp',

#### First group
{
    'app': 'webapp', 
    'label': 'group1',
    'models': (
        'webapp.ProductModelName_1', 
        'webapp.ProductModelName_2', 
        'webapp.ProductModelName_3',

    )
},
# Second group: same app, but different label
{
    'app': 'webapp', 
    'label': 'group2',
    'models': (
        'webapp.Model_X', 
        'webapp.Model_Y',
    )
},
)

Upvotes: 1

Jarno
Jarno

Reputation: 7212

I do not think that splitting up your business logic, i.e. your app, when all you want to achieve is some kind of markup is the right way. Instead I found the Python package django-modeladmin-reorder that lets you achieve this easily. You can combine its feature of labelling apps and reordering models to group models of one app in the admin. After following the installation instructions, add something like this to your settings.py

ADMIN_REORDER = (
    # First group
    {'app': 'myapp', 'label': 'Group1',
     'models': ('myapp.Model_1',
                'myapp.Model_4',)
    },
    # Second group: same app, but different label
    {'app': 'myapp', 'label': 'Group2',
     'models': ('myapp.Model_2',
     'myapp.Model_3',)
    },)

Upvotes: 34

Edwin Lunando
Edwin Lunando

Reputation: 2816

You need to create two apps. first app == Group 1. second app == Group 2.

Then, you need to create proxy model in your new app. Something like this.

class ProxyReview(Review):
    class Meta:
        proxy = True    
        # If you're define ProxyReview inside review/models.py,
        #  its app_label is set to 'review' automatically.
        # Or else comment out following line to specify it explicitly               
        # app_label = 'review'

        # set following lines to display ProxyReview as Review
        # verbose_name = Review._meta.verbose_name
        # verbose_name_plural = Review._meta.verbose_name_plural


# in admin.py
admin.site.register(ProxyReview)

Upvotes: -6

Related Questions