Automate createsuperuser command in Django

Vaibhav Vaibhav Oct 17, 2021 May 22, 2021 Django Django User
  1. Create Superuser Using the Django Shell
  2. Create Superuser Using a Python Script
Automate createsuperuser command in Django

In Django, the user that has administrator rights is known as a superuser. Superusers can do everything. They can create new users, delete existing users, manipulate information of other users, etc.

In Django, to create a superuser, we generally use a command line command, python createsuperuser, followed by a few more inputs: username, email, and password.

But if we could automate this whole process or complete it using a single line of code, wouldn’t that be amazing?

In this article, we will learn to automate superuser creation.

Create Superuser Using the Django Shell

Django Shell is a command-line Python tool to interact with the database using Django’s database APIs. Using Django Shell, we can create data in the database, modify existing data, and even delete existing data. Moreover, we can also create new users and super users.

To create a superuser using Django Shell, use the following script.

echo "from django.contrib.auth import get_user_model; User = get_user_model(); User.objects.create_superuser('userUsername', 'userEmail', 'userPassword')" | python shell

This script first imports a method, get_user_model, to get the user model used by Django.

This method retrieves a reference to the actual user model object and stores it in the variable User.

Using this reference, it then creates a new superuser using create_superuser(). This method accepts a username, an email, and a password.

Note that these fields are valid for Django’s default user model. If you use a custom user model, then the fields to create a user or a superuser will change accordingly.

Create Superuser Using a Python Script

Since Django models are accessible everywhere in the Django project, we can use them to create users and superusers. We can create a function that accepts some inputs and then creates a superuser using them for this task.

from django.contrib.auth.models import User

def createSuperUser(username, password, email = "", firstName = "", lastName = "")
    invalidInputs = ["", None]

    if username.strip() in invalidInputs or password.strip() in invalidInputs:
        return None

    user = User(
        username = username,
        email = email,
        first_name = firstName,
        last_name = lastName,
    user.is_superuser = True
    user.is_staff = True

    return user

This function has two compulsory arguments, namely, username and password, and three optional arguments. It creates a superuser based on the values passed and returns a reference to the newly formed superuser.

Vaibhav Vaibhav avatar Vaibhav Vaibhav avatar

Vaibhav is an artificial intelligence and cloud computing stan. He likes to build end-to-end full-stack web and mobile applications. Besides computer science and technology, he loves playing cricket and badminton, going on bike rides, and doodling.

LinkedIn GitHub

Related Article - Django User

  • Check the Logged in User in Django