Search code examples
djangodjango-modelsdjango-admin

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.


Solution

  • 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