Django: How to Create an Admin User with Fixture

created:

updated:

tags: django backend

Sometimes we need fixture data for users when we load our database. We can use Django’s loaddata command to do so:

  1. Create a fixture folder under an app (ex: users/fixtures)
  1. If it is a superuser, ensure to use Django’s built-in make_password function
  • This is one example of how to achive this:
from django.contrib.auth.hashers import make_password

password_hash = make_password("test")
  1. Create a fixture file (ex: users/fixtures/initial_users.json)
[
    {
        "model": "users.User",
        "pk": 1,
        "fields": {
            "first_name": "Admin",
            "last_name": "WoodLover",
            "username": "[email protected]",
            "email": "[email protected]",
            "password": "pbkdf2_sha256$720000$lPPZdS0P0oxtqGtG5mFvLY$4ZuhvptRszz3cSz2ioHzjhPxwLodVth6J/un5P6sUvw=",
            "is_admin": "True",
            "is_superuser": "True",
            "is_active": "True"
        }
    }
]
  1. Load data to the database via python manage.py loaddata initial_users

References