Create a Management Command to add User in Django

If you want to create a user in Django who does not have access to the admin panel, you can use the create_user method instead of createsuperuser. The create_user method creates a regular user account without admin privileges.

You can create a custom management command to add users. Create a new file management/commands/create_user.py in one of your apps (e.g., myapp):

# myapp/management/commands/create_user.py

from django.core.management.base import BaseCommand
from django.contrib.auth import get_user_model

class Command(BaseCommand):
    help = 'Create a regular user'

    def add_arguments(self, parser):
        parser.add_argument('username', type=str)
        parser.add_argument('email', type=str)
        parser.add_argument('password', type=str)

    def handle(self, *args, **kwargs):
        username = kwargs['username']
        email = kwargs['email']
        password = kwargs['password']

        User = get_user_model()
        user = User.objects.create_user(username=username, email=email, password=password)
        user.save()

        self.stdout.write(self.style.SUCCESS(f'User {username} created successfully'))

After creating this file, you can run the management command:

python manage.py create_user username [email protected] password