How to automate createsuperuser on django?

后端 未结 16 1551
情歌与酒
情歌与酒 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.

    0 讨论(0)
  • 2020-11-29 16:32

    A solution based on Adam Charnock's approach above is available as a Python package by now. It takes three steps:

    1. Install: pip install django-createsuperuserwithpassword

    2. Activate: INSTALLED_APPS += ("django_createsuperuserwithpassword", )

    3. Apply:

      python manage.py createsuperuserwithpassword \
              --username admin \
              --password admin \
              --email admin@example.org \
              --preserve
      

    That's it.

    0 讨论(0)
  • 2020-11-29 16:33

    Go to command prompt and type:

    C:\WINDOWS\system32>pip install django-createsuperuser
    Collecting django-createsuperuser
      Downloading https://files.pythonhosted.org/packages/93/8c/344c6367afa62b709adebee039d09229675f1ee34d424180fcee9ed857a5/django-createsuperuser-2019.4.13.tar.gz
    Requirement already satisfied: Django>1.0 in c:\programdata\anaconda3\lib\site-packages (from django-createsuperuser) (2.2.1)
    Requirement already satisfied: setuptools in c:\programdata\anaconda3\lib\site-packages (from django-createsuperuser) (41.0.1)
    Requirement already satisfied: sqlparse in c:\programdata\anaconda3\lib\site-packages (from Django>1.0->django-createsuperuser) (0.3.0)
    Requirement already satisfied: pytz in c:\programdata\anaconda3\lib\site-packages (from Django>1.0->django-createsuperuser) (2018.7)
    Building wheels for collected packages: django-createsuperuser
      Running setup.py bdist_wheel for django-createsuperuser ... done
      Stored in directory: C:\Users\Arif Khan\AppData\Local\pip\Cache\wheels\0c\96\2a\e73e95bd420e844d3da1c9d3e496c92642a4f2181535440db2
    Successfully built django-createsuperuser
    Installing collected packages: django-createsuperuser
    

    if not executed the migration then go to django application folder and execute following

    1. python manage.py migrate
    2. python manage.py createsuperuser

    then bingo.

    0 讨论(0)
  • 2020-11-29 16:33
    python manage.py shell -c "from django.contrib.auth.models import User; \
                               User.objects.filter(username='admin1').exists() or \
                               User.objects.create_superuser('admin1',
                               'admin1@example.com', 'admin1')"
    
    0 讨论(0)
  • 2020-11-29 16:36

    With shell_plus it's much easier actually

    echo "User.objects.create_superuser('test@test.com', 'test')" | python manage.py shell_plus
    

    As others mentioned, with Django 3.0 you can pass the credentials via environment variables. However this approach is much more flexible since it allows you to do any other more complicated task like removing all tests users, etc.

    0 讨论(0)
  • 2020-11-29 16:43

    I used Tk421 one liner but got an error message as: 1) I think I am using a later version of Django (1.10) Manager isn't available; 'auth.User' has been swapped for 'users.User' 2) the order of the parameters to create_superuser was wrong.

    So I replaced it with:

    echo "from django.contrib.auth import get_user_model; User = get_user_model(); User.objects.filter(email='admin@example.com', is_superuser=True).delete(); User.objects.create_superuser('admin', 'admin@example.com', 'nimda')" | python manage.py shell
    

    and what I as really pleased with is that it works on a heroku deployment as well:

    heroku run echo "from django.contrib.auth import get_user_model; User = get_user_model(); User.objects.filter(email='admin@example.com', is_superuser=True).delete(); User.objects.create_superuser('admin', 'admin@example.com', 'nimda')" | python manage.py shell
    

    This will work nicely repeatedly. I am using it the beginning of a project so don't worry about the terrible cascaded deletes that might occur later.

    I have revisited after some trouble with running this inside local() from fabric. what seemed to be happening is that the pipe symbol mean that it was getting interpreted locally rather than on heroku. To sort this I wrapped in the command in quotes. Then had to used triple double quotes for the python strings inside the single quotes of the whole python command.

    heroku run "echo 'from django.contrib.auth import get_user_model; User = get_user_model(); User.objects.filter(email="""admin@example.com""", is_superuser=True).delete(); User.objects.create_superuser("""admin""", """admin@example.com""", """nimda""")' | python manage.py shell"
    
    0 讨论(0)
提交回复
热议问题