How to automate createsuperuser on django?

后端 未结 16 1573
情歌与酒
情歌与酒 2020-11-29 15:57

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

How can I get this?

16条回答
  •  生来不讨喜
    2020-11-29 16:23

    You could write a simple python script to handle the automation of superuser creation. The User model is just a normal Django model, so you'd follow the normal process of writing a stand-alone Django script. Ex:

    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()
    

    You can also pass createsuperuser a few options, namely --noinput and --username, which would let you automatically create new superusers, but they would not be able to login until you set a password for them.

提交回复
热议问题