How to move a model to another section in Django admin

Is it possible to transfer the default group model from the "Authentication and Authorization" section (on the Django admin site) to a custom one and how to do it?

Let them start from the beginning in other words.

My Django project has very simple accounts applications.

File

models.py looks like this:

from django.contrib.auth.models import AbstractUser class User(AbstractUser): def __str__(self): return self.email 

file serializers.py:

 from rest_framework import serializers from django.contrib.auth.models import Group from django.contrib.auth import get_user_model class GroupSerializer(serializers.HyperlinkedModelSerializer): class Meta: model = Group class UserSerializer(serializers.HyperlinkedModelSerializer): groups = serializers.HyperlinkedRelatedField( many=True, required=False, read_only=True, view_name="group-detail" ) class Meta: model = get_user_model() exclude = ('user_permissions',) 

Now on the administrative site there are two sections: “Accounts” and “Authentication and authorization”. The Accounts section contains my Users table (for the user model) and the Authentication and Authorization section contains the Groups table (for the default Django group authorization model).

My question is: is it possible and how to move the table (model) to the "Accounts" section?

I even tried to create a custom “Group” model based on the default auth Group model of Django, but was stuck with migration exceptions.

+5
source share
1 answer

Is it possible to transfer the default group model from the "Authentication and Authorization" section (on the Django admin site) to a custom one and how to do it?

Yes it is possible.

1) You can move your model to the auth section, just add to your class:

 class Meta: app_label = 'auth' 

2) You can move the group and user models to your part of the application, for this option you need:

Redefine the user model and add it to your application

 from django.contrib.auth.models import AbstractUser class CustomUser(AbstractUser): pass 

you also need to add AUTH_USER_MODEL = 'your_app.CustomUser' your project AUTH_USER_MODEL = 'your_app.CustomUser'

Remember to declare in admin.py from your application:

 class UserAdmin(admin.ModelAdmin): pass admin.site.register(CustomUser, UserAdmin) 

For the group model, enter this code in admin.py:

 from django.db.models.loading import get_models from django.contrib.auth import models models = get_models(models) models[1]._meta.app_label = 'your_app' 

3) You can look at django-admin-tools

+6
source

Source: https://habr.com/ru/post/1211802/


All Articles