[Django]-How to group models in django admin?

-6๐Ÿ‘

โœ…

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)
๐Ÿ‘คEdwin Lunando

32๐Ÿ‘

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',)
    },)
๐Ÿ‘คJarno

-1๐Ÿ‘

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',
    )
},
)
๐Ÿ‘คAriful Haque

Leave a comment