如何在 Django 上自动创建超级用户? [英] How to automate createsuperuser on django?

查看:54
本文介绍了如何在 Django 上自动创建超级用户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 django 上自动运行 manage.py createsuperuser 但它似乎无法设置默认密码.

I want to auto run manage.py createsuperuser on django but it seams that there is no way of setting a default password.

我怎样才能得到这个?它必须独立于django数据库.

How can I get this? It has to be independent on the django database.

推荐答案

如果您直接引用 User,您的代码将无法在 AUTH_USER_MODEL 设置已更改为不同用户模型的项目中工作.创建用户的更通用方法是:

If you reference User directly, your code will not work in projects where the AUTH_USER_MODEL setting has been changed to a different user model. A more generic way to create the user would be:

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

原答案

这里有一个简单版本的脚本来创建超级用户:

Here there is a simple version of the script to create a superuser:

echo "from django.contrib.auth.models import User; User.objects.create_superuser('admin', 'admin@example.com', 'pass')" | python manage.py shell

这篇关于如何在 Django 上自动创建超级用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆