我想在django上自动运行manage.py createsuperuser,但是没有办法设置默认密码。
我怎么才能得到这个?它必须独立于django数据库。
我想在django上自动运行manage.py createsuperuser,但是没有办法设置默认密码。
我怎么才能得到这个?它必须独立于django数据库。
当前回答
如果您直接引用User,您的代码将无法在AUTH_USER_MODEL设置已更改为不同用户模型的项目中工作。创建用户的一种更通用的方法是:
echo "from django.contrib.auth import get_user_model; User = get_user_model(); User.objects.create_superuser('admin', 'admin@myproject.com', 'password')" | python manage.py shell
原来的答案
这里有一个简单的脚本版本来创建一个超级用户:
echo "from django.contrib.auth.models import User; User.objects.create_superuser('admin', 'admin@example.com', 'pass')" | python manage.py shell
其他回答
这是我为Heroku post_deploy和一个预定义的app.json变量拼凑起来的:
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
这样你可以有一个单独的env变量:
CREATE_SUPER_USER=admin:admin@example.com:password
我喜欢shell——command选项,但不确定如何在命令脚本中获得换行符。如果没有换行符,if表达式会导致语法错误。
我这样解决了这个问题。
Wsgi.py文件总是在django项目启动时运行。如果它不存在,我运行create super user命令。
import os
from django.contrib.auth.models import User
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', {settings_file})
application = get_wsgi_application()
users = User.objects.all()
if not users:
User.objects.create_superuser(username="username", email="user@example.com", password="password", is_active=True, is_staff=True)
这里可以添加一个函数。例如;如果这个user1不存在,添加user1。
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')"
用shell_plus就简单多了
echo "User.objects.create_superuser('test@test.com', 'test')" | python manage.py shell_plus
正如其他人提到的,在Django 3.0中,你可以通过环境变量来传递凭证。然而,这种方法更加灵活,因为它允许您执行任何其他更复杂的任务,如删除所有测试用户等。
我使用了Tk421一行,但得到了一个错误消息:1)我认为我使用的是Django(1.10)的更新版本,Manager不可用;的身份验证。用户'已被替换为'users '。User' 2) create_superuser的参数顺序错误。
所以我把它换成:
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
我真正满意的是,它也适用于英雄部署:
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
这将很好地重复工作。我在一个项目的开始使用它,所以不用担心以后可能发生的可怕的级联删除。
我已经重新访问了一些麻烦后运行这个内部local()从织物。似乎发生的事情是,管子符号意味着它是在局部而不是在heroku上得到解释。为了对它进行排序,我用引号括在命令中。然后必须在整个python命令的单引号内对python字符串使用三双引号。
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"