我想在django上自动运行manage.py createsuperuser,但是没有办法设置默认密码。
我怎么才能得到这个?它必须独立于django数据库。
我想在django上自动运行manage.py createsuperuser,但是没有办法设置默认密码。
我怎么才能得到这个?它必须独立于django数据库。
当前回答
非常简单,监听post syncdb信号,从配置文件中读取超级用户凭证并应用它。
查看django-finalware和它的前身django-bootup[已弃用]
其他回答
从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添加到脚本和管道中最方便的方法。
我自己也在寻找答案。我决定创建一个Django命令,它扩展了基本的createsuperuser命令(GitHub):
from django.contrib.auth.management.commands import createsuperuser
from django.core.management import CommandError
class Command(createsuperuser.Command):
help = 'Crate a superuser, and allow password to be provided'
def add_arguments(self, parser):
super(Command, self).add_arguments(parser)
parser.add_argument(
'--password', dest='password', default=None,
help='Specifies the password for the superuser.',
)
def handle(self, *args, **options):
password = options.get('password')
username = options.get('username')
database = options.get('database')
if password and not username:
raise CommandError("--username is required if specifying --password")
super(Command, self).handle(*args, **options)
if password:
user = self.UserModel._default_manager.db_manager(database).get(username=username)
user.set_password(password)
user.save()
使用示例:
./manage.py createsuperuser2 --username test1 --password 123321 --noinput --email 'blank@email.com'
这样做的优点是仍然支持默认命令的使用,同时还允许使用非交互式的方式指定密码。
我建议运行一个数据迁移,这样当迁移应用到项目中时,超级用户将作为迁移的一部分被创建。用户名和密码可以设置为环境变量。这在容器中运行应用程序时也很有用(以这个线程为例)。
您的数据迁移将看起来像这样:
import os
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('<your_app>', '<previous_migration>'),
] # can also be emtpy if it's your first migration
def generate_superuser(apps, schema_editor):
from django.contrib.auth.models import User
DJANGO_SU_NAME = os.environ.get('DJANGO_SU_NAME')
DJANGO_SU_EMAIL = os.environ.get('DJANGO_SU_EMAIL')
DJANGO_SU_PASSWORD = os.environ.get('DJANGO_SU_PASSWORD')
superuser = User.objects.create_superuser(
username=DJANGO_SU_NAME,
email=DJANGO_SU_EMAIL,
password=DJANGO_SU_PASSWORD)
superuser.save()
operations = [
migrations.RunPython(generate_superuser),
]
希望有帮助!
编辑: 有些人可能会提出这样的问题:如何设置这些环境变量并让Django知道它们。有很多方法,在其他SO帖子中已经回答过了,但是作为一个快速的指针,创建一个.env文件是一个好主意。然后您可以使用python-dotenv包,但是如果您已经使用pipenv设置了一个虚拟环境,它将自动在您的.env文件中设置envars。同样地,通过docker-compose运行你的应用程序可以读取你的.env文件。
这是我为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表达式会导致语法错误。
进入命令提示符并输入:
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
如果没有执行迁移,那么转到django应用程序文件夹并执行以下命令
Python manage.py迁移 Python manage.py创建超级用户
宾果。