Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing the group of a model in Django Admin

For whatever reason I have three models related to authentication, but in Django Admin they show up in two different groups. For example:

AUTHORIZATION
-------------
Security Questions
Users

AUTHORIZATION AND AUTHENTICATION
--------------------------------
Groups

Seems like they should be under one group and I would like to move them to be under one group.

I came across this Q/A from a few years ago:

Adding a model in Django Admin to the User/Group models?

Wonder if there is an easier way now in Django 1.11 like using a class Meta: in the model or in admin.py. Looking through the documentation and haven't come across anything yet.

like image 337
cjones Avatar asked Oct 10 '17 18:10

cjones


2 Answers

I actually had to combine what both Mohammad and NeErAj suggested.

When I tried to move Groups to the Authorization section, which is an app containing a custom User model, it created duplicates of the Groups. Django still wanted to insert the default auth_group which I couldn't figure out how to get rid of.

# ./models.py
from django.contrib.auth.models import Group

class Group(Group):
    pass

    class Meta:
    app_label = 'authentication'

# ./admin.py
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from .models import User, SecurityQuestions, Group

admin.site.register(User, UserAdmin)
admin.site.register(SecurityQuestions)
admin.site.register(Group)

AUTHORIZATION
-------------
Groups
Security Questions
Users

AUTHORIZATION AND AUTHENTICATION
--------------------------------
Groups

Since I was using a custom User model, I figured it would be easier to move them to app_label = 'auth'. That way I wouldn't have to fight with the default auth_group. Ended up doing the following:

# ./models.py
from django.contrib.auth.models import AbstractUser

class User(AbstractUser):
    ...

    class Meta:
        db_table = 'Users'

class SecurityQuestions(models.Model):
    ...

    class Meta:
        app_label = 'auth'
        db_table = 'Security_Questions'
        verbose_name = 'Security Question'
        verbose_name_plural = 'Security Questions'

 class ProxyUser(User):
     pass

     class Meta:
         app_label = 'auth'
         proxy = True
         verbose_name = 'User'
         verbose_name_plural = 'Users'

# ./admin.py

from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from .models import User, SecurityQuestions, ProxyUser

admin.site.register(ProxyUser, UserAdmin)
admin.site.register(SecurityQuestions)

This got everything to look like:

AUTHORIZATION AND AUTHENTICATION
--------------------------------
Groups
Security Questions
Users
like image 170
cjones Avatar answered Sep 22 '22 18:09

cjones


The simplest solution is to use the django-modeladmin-reorder: it allows you to reorder and merge apps in the admin site.

Here is the configuration that you'd need for this particular example:

ADMIN_REORDER = [
    {
        'app': 'auth',
        'models': [
            'my_custom_auth_app.User',
            'my_custom_auth_app.SecurityQuestions',
            'auth.Group',
        ]
    }
]

Behind the scenes, this app uses a middleware that changes the app_list context when rendering the admin templates.

like image 30
Benoit Blanchon Avatar answered Sep 22 '22 18:09

Benoit Blanchon