如何从 fixture 在 Django 中添加超级用户

How to add superuser in Django from fixture

如何通过 Django fixtures 添加超级用户(不仅仅是用户)?

假设我想要 login:admin、password:admin。

解决方案 1

空数据库:

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

并且在 users.json 你需要固定装置。

解决方案 2

./manage.py shell

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

并创建灯具文件:

[
    { "model": "auth.user",
        "pk": 1,
        "fields": {
            "username": "admin",
            "password": "pbkdf2_sha256000$vkRy7QauoLLj$ry+3xm3YX+YrSXbri8s3EcXDIrx5ceM+xQjtpLdw2oE="
            "is_superuser": true,
            "is_staff": true,
            "is_active": true
        }
    }
]

如果您正在使用 pytest-django you can use the existing fixture admin_user

来自 RTD:

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