如何使用Linux shell脚本在Django中创build超级用户?

最近,我正在用Docker部署一个应用程序。我遇到了一个关于如何编写将在Linux上执行的启动脚本的问题。 一个例子是作为同胞:

通常,我们创build这样的超级用户:

iMac:mysite yetongxue$ python manage.py createsuperuser Username (leave blank to use 'yetongxue'): root Email address: example@qq.com Password: Password (again): Superuser created successfully. 

但是,当使用docker部署应用程序时,我必须将这个命令写入一个脚本,该脚本将在Docker容器启动后执行。如何在脚本中设置用户名,电子邮件和密码? 谢谢!

 #!/bin/bash python manage.py createsuperuser 

而不是使用createsuperuserpipe理命令,您可以提供一个夹具 。

您需要为auth.user模型提供一个固定装置。 对于密码,您可以通过Django shell生成密码

 >>> from django.contrib.auth.hashers import make_password >>> make_password('password') 

你的最终装置应该是这样的:

 [ { "model": "auth.user", "pk": 1, "fields": { "username": "admin", "password": "<output of make_password()>" "is_superuser": true, "is_staff": true, "is_active": true } } ]