Django: how to bulk create users
One task that I receive most often is to create a list of users in the app. The common way I'm using is to use Jupyter Notebook to clean the user list first and turn them into a python list. Then I use python manage.py shell_plus
to write the users into the app.
First, we need to import a hasher from django auth module:
from django.contrib.auth.hashers import make_password
Secondly, input the user list and password that we want to create.
Lastly, we just need to use User object to bulk create our new users.
User.objects.bulk_create([ User( username=name, email=name, password=make_password(password), is_active=True, ) for name in user_list ])
In the user list, we can also add password or other user information like Country or birthday. As a demo, we set the username the same as the email.