[Fixed]-How to programmably create an User in Django?

38👍

You can create user from command line using below method:

user@hostname$ python3 -m django shell
>>> import django.contrib.auth
>>> User = django.contrib.auth.get_user_model()
>>> user = User.objects.create_user(username='username', password='userpassword')

Observations:

  1. A User created, is available for the whole project and not just a single app
  2. By default, is_superuser and is_staff are both False when a User is created unless they are overwritten by passing in create_user() method.

2👍

If you haver extended the base user model then change the first command in @Astik Anand answer to:

from django.contrib.auth import get_user_model

User = get_user_model()

Leave a comment