我想在django上自动运行manage.py createsuperuser,但是没有办法设置默认密码。
我怎么才能得到这个?它必须独立于django数据库。
我想在django上自动运行manage.py createsuperuser,但是没有办法设置默认密码。
我怎么才能得到这个?它必须独立于django数据库。
当前回答
你可以像这样在自定义命令中创建一个超级用户:
import os
from django.contrib.auth.models import User
from django.core.management import BaseCommand, call_command
from immo_project import settings
class Command(BaseCommand):
def handle(self, *args, **options):
call_command('createsuperuser', interactive=False, username='admin', email='test@example.com')
user = User.objects.get(username='admin')
user.set_password('password')
user.save()
其他回答
从Django 3.0开始,你可以使用默认的createsuperuser——noinput命令,并将所有必需的字段(包括密码)设置为环境变量DJANGO_SUPERUSER_PASSWORD, DJANGO_SUPERUSER_USERNAME, DJANGO_SUPERUSER_EMAIL。——不需要input标志。
这来自最初的文档:https://docs.djangoproject.com/en/3.0/ref/django-admin/#django-admin-createsuperuser
这是将createsuperuser添加到脚本和管道中最方便的方法。
您可以编写一个简单的python脚本来处理超级用户创建的自动化。User模型只是一个普通的Django模型,所以你要遵循编写独立Django脚本的正常过程。例:
import django
django.setup()
from django.contrib.auth.models import User
u = User(username='unique_fellow')
u.set_password('a_very_cryptic_password')
u.is_superuser = True
u.is_staff = True
u.save()
您还可以向createsuperuser传递一些选项,即——noinput和——username,这将允许您自动创建新的超级用户,但他们将无法登录,直到您为他们设置密码。
对于那些只想在AWS Elastic Beanstalk上托管django网站的人(即使没有docker),并且被超级用户部分卡住了,在.platform > hooks > postdeploy中创建一个名为01_migration .sh的文件,并输入以下内容:
#!/bin/bash
source /var/app/venv/*/bin/activate && { python migrate.py createsuperuser --noinput; }
然后,您可以将DJANGO_SUPERUSER_PASSWORD, DJANGO_SUPERUSER_USERNAME, DJANGO_SUPERUSER_EMAIL添加到应用程序环境的配置部分。
然后将以下文件添加到.ebextentions > django.config文件夹中
container_commands:
01_chmod1:
command: "chmod +x .platform/hooks/postdeploy/01_migrate.sh"
这将以一种安全的方式创建超级用户,使用相同的逻辑,您还可以通过添加到01_migration .sh文件来运行迁移和collectstatic。
我使用'./manage.py shell -c':
./manage.py shell -c "from django.contrib.auth.models import User; User.objects.create_superuser('admin', 'admin@example.com', 'adminpass')"
它不使用额外的echo,这样做的好处是你可以将它传递给docker容器执行。不需要使用sh -c“…”,这会让你逃离地狱。
记住,首先是用户名,然后是电子邮件。
如果你有一个自定义的用户模型,你需要导入它,而不是auth.models.User
我使用了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"