django sub admin panel


I am working on the Django admin panel but now client is asking me that he need a functionality to create sub admin from the admin panel and provide some permission to sub admin
flow –
Here admin need a tab like sub admin management- admin will be able to create new admin by providing email, name photo, once admin submit the details of the sub admin link will be shared to the sub admin inputted email from where he can set his password,
admin will also have the functionality to provide different permission to different sub admin, will be able to change these permission in future
i have 6 application in admin where admin is admin to perform cur operation on user management, device management. i.e permission for sub admin will be for example sub admin can view user management but view and edit device management and so on.


  1. How we can register the sub admin? i.e to register the super admin we go to terminal and write the commands to register the sub admin now registration for sub admin is done from admin panel
  2. How we can provide different permission to the sub admin
  3. Do I need to create a new project that will contain the functionality of sub admin?

i have done a little R&D on the same I got one solution

  • 2 level admin panel
Asked By: Prince



Create User and give permission to that user. You can define which module that user can access.

You can create permission group and assign or you can directly assign permissions to the new user , so that they will be able to access given access controls only.

To send email and allow subuser to update the task you can first create a model:

from django.db import models
from django.contrib.auth.models import User

class SubAdmin(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    photo = models.ImageField(upload_to='sub_admin_photos/')
    permissions = models.ManyToManyField(Permission, blank=True)

    def __str__(self):

and then register newly created model in you dajngo admin panel:

from django.contrib import admin
from django.contrib.auth.models import User
from django.contrib.auth.admin import UserAdmin
from .models import SubAdmin

class SubAdminInline(admin.StackedInline):
    model = SubAdmin
    can_delete = False

class CustomUserAdmin(UserAdmin):
    inlines = (SubAdminInline, ), CustomUserAdmin)

You will need custom views to send email to your sub user:

from django.contrib.auth.tokens import default_token_generator
from django.utils.encoding import force_bytes
from django.utils.http import urlsafe_base64_encode
from django.template.loader import render_to_string
from django.core.mail import EmailMessage
from django.conf import settings

def send_sub_admin_invite_email(sub_admin):
    subject = 'Invitation to join the team'
    token_generator = default_token_generator
    uid = urlsafe_base64_encode(force_bytes(
    token = token_generator.make_token(sub_admin.user)
    message = render_to_string('sub_admin_invite_email.html', {
        'sub_admin': sub_admin,
        'uid': uid,
        'token': token,
    email = EmailMessage(subject, message, to=[])
    email.content_subtype = 'html'

To assign different roles by super admin to your sub admin you will need a form:

from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm
from .models import SubAdmin

class SubAdminCreationForm(UserCreationForm):
    photo = forms.ImageField()
    permissions = forms.ModelMultipleChoiceField(

    class Meta:
        model = User
        fields = ('email', 'first_name', 'last_name', 'photo', 'permissions')

    def save(self, commit=True):
        user = super().save(commit=False)
        user.is_staff = True
        user.is_active = False
        if commit:
            sub_admin = SubAdmin.objects.create(user=user, photo=self.cleaned_data.get('photo'))
        return user

On my case mostly I used to use Django built-in permissions to manage the permissions:

 from django.contrib.auth.decorators import user_passes_test

def sub_admin_required(view_func):
    decorated_view_func = user_passes_test(
        lambda u: u.is_active and u.sub_admin, login_url='/admin/login/')
    return decorated_view_func(view_func)

def sub_admin_dashboard(request):
    return render(request, 'sub_admin_dashboard.html')

This is my rough idea on how you can create sub-admin and assign different roles while giving them opportunity to update their profile related things.

Answered By: Madhav Dhungana
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.