How to add superuser in Django from fixture
Asked Answered
E

2

21

How to add SUPERuser(not just user) through Django fixtures?

Let's say I wanna have login:admin, password:admin.

Expressive answered 16/12, 2015 at 20:4 Comment(5)
Possible duplicate of Users in initial data fixtureDustpan
This is on page 2 of the django tutorial...Rube
@TomaszJakubRup I'm talking about superuser, not usual user.Expressive
@Rube I'm looking for solution, which uses fixtures, not manual inputExpressive
@Expressive A superuser is a normal user with the is_superuser flag set.Unbowed
D
41

solution 1

On empty database:

python manage.py createsuperuser
python manage.py dumpdata auth.User --indent 4 > users.json

and in users.json You have needed fixtures.

solution 2

./manage.py shell

>>> from django.contrib.auth.hashers import make_password
>>> make_password('test')
'pbkdf2_sha256$10000$vkRy7QauoLLj$ry+3xm3YX+YrSXbri8s3EcXDIrx5ceM+xQjtpLdw2oE='

and create fixtures file:

[
    { "model": "auth.user",
        "pk": 1,
        "fields": {
            "username": "admin",
            "password": "pbkdf2_sha256$10000$vkRy7QauoLLj$ry+3xm3YX+YrSXbri8s3EcXDIrx5ceM+xQjtpLdw2oE="
            "is_superuser": true,
            "is_staff": true,
            "is_active": true
        }
    }
]
Dustpan answered 16/12, 2015 at 21:27 Comment(1)
This is what i've been looking for. Why did I get minuses?)Expressive
S
0

If you are using pytest-django you can use the existing fixture admin_user.

From RTD:

An instance of a superuser, with username “admin” and password “password” (in case there is no “admin” user yet).

Stralsund answered 21/9, 2018 at 13:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.