How to automate createsuperuser on django?

后端 未结 16 1553
情歌与酒
情歌与酒 2020-11-29 15:57

I want to auto run manage.py createsuperuser on django but it seams that there is no way of setting a default password.

How can I get this?

16条回答
  •  [愿得一人]
    2020-11-29 16:29

    This is what I cobbled together for Heroku post_deploy and a predefined app.json variable:

    if [[ -n "$CREATE_SUPER_USER" ]]; then
        echo "==> Creating super user"
        cd /app/example_project/src
        printf "from django.contrib.auth.models import User\nif not User.objects.exists(): User.objects.create_superuser(*'$CREATE_SUPER_USER'.split(':'))" | python /app/example_project/manage.py shell
    fi
    

    With this you can have a single env variable:

    CREATE_SUPER_USER=admin:admin@example.com:password
    

    I like the shell --command option, but not sure how the get newline character in the command script. Without the newline the if expression results in syntax error.

提交回复
热议问题