Sometimes we need fixture data for users when we load our database.
We can use Django’s loaddata
command to do so:
- Create a fixture folder under an app (ex:
users/fixtures
)
- 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")
- 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"
}
}
]
- Load data to the database via
python manage.py loaddata initial_users