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

Solution 1:

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.

Solution 2:

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

Solution 3:

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/

Solution 4:

If you want the ability — as I do — to really start with a fresh database without getting asked that superuser question, then you can just de-register the signal handler that asks that question. Check out the very bottom of the file:

django/contrib/auth/management/__init__.py

to see how the registration of the superuser function gets performed. I found that I could reverse this registration, and never get asked the question during "syncdb", if I placed this code in my "models.py":

from django.db.models import signals
from django.contrib.auth.management import create_superuser
from django.contrib.auth import models as auth_app

# Prevent interactive question about wanting a superuser created.  (This
# code has to go in this otherwise empty "models" module so that it gets
# processed by the "syncdb" command during database creation.)

signals.post_syncdb.disconnect(
    create_superuser,
    sender=auth_app,
    dispatch_uid = "django.contrib.auth.management.create_superuser")

I am not sure how to guarantee that this code gets run after the Django code that does the registration. I had thought that it would depend on whether your app or the django.contrib.auth app gets mentioned first in INSTALLED_APPS, but it seems to work for me regardless of the order I put them in. Maybe they are done alphabetically and I'm lucky that my app's name starts with a letter later than "d"? Or is Django just smart enough to do its own stuff first, then mine in case I want to muck with their settings? Let me know if you find out. :-)

Solution 5:

I've overcome this feature using south

Its a must have for any django developer.

South is a tool designed to help migrate changes over to the live site without destroying information or database structure. The resulting changes can be tracked by south and using the generated python files - can perform the same actions on an alternative database.

During development, I use this tool to git track my database changes - and to make a change to the database without the need to destroy it first.

  1. easy_install south
  2. Add 'south' to your installed apps

Proposing first time run of south on an app.

$ python manage.py schemamigration appname --init

This will initiate schema detection on that app.

$ python manage.py migrate appname

This will apply the model changes

  • The database will have the new models.

Changing a model after the first run

$ python manage.py schemamigration appname --auto

$ python manage.py migrate appname


Models will have changed - data is not destroyed. Plus south does much more...