Automatically create an admin user when running Django's ./manage.py syncdb

I know the question has been answered already but ...

A Much simpler approach is to dump the auth module data into a json file once the superuser has been created:

 ./manage.py dumpdata --indent=2 auth > initial_data.json

You can also dump the sessions data:

./manage.py dumpdata --indent=2 sessions

You can then append the session info to the auth module dump (and probably increase the expire_date so it does not expire... ever ;-).

From then, you can use

/manage.py syncdb --noinput

to load the superuser and his session when creating the db with no interactive prompt asking you about a superuser.


Instead of deleting your entire database, just delete the tables of your app before running the syncdb

This will accomplish it for you in a single line (per app):

python manage.py sqlclear appname | python manage.py dbshell

The first command will look at your app and generate the required SQL to drop the tables. This output is then piped to the dbshell to execute it.

After its done, run your syncdb to recreate the tables:

python manage.py syncdb

The key is to use --noinput at the time of syncdb & then use this one liner to create superuser

echo "from django.contrib.auth.models import User; User.objects.create_superuser('myadmin', '[email protected]', 'hunter2')" | python manage.py shell

Credit : http://source.mihelac.org/2009/10/23/django-avoiding-typing-password-for-superuser/