views:

890

answers:

6

My project is in early development. I frequently delete the database and run manage.py syncdb to set up my app from scratch.

Unfortunately, this always pops up:

You just installed Django's auth system, which means you don't have any superusers defined.
Would you like to create one now? (yes/no):

Then you have supply a username, valid email adress and password. This is tedious. I'm getting tired of typing test\[email protected]\ntest\ntest\n.

How can I automatically skip this step and create a user programatically when running manage.py syncdb ?

+1  A: 

My solution to this was to just not delete that auth tables when wiping out my database.

nbv4
+8  A: 

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
Andre Miller
I like this answer. Thanks for the suggestion!
ropable
+1  A: 

Take a look at the dumpdata management command. For instance:

python manage.py dumpdata > initial_data.json

If this file, called a fixture, is named initial_data (.xml or .json), then the syncdb command will pick it up and populate your tables accordingly. It will still ask you if you want to create a user, but I believe you may safely answer "no", after which point it will populate the database based on your fixture.

More info on this can be found in the docs.

Ryan Duffield
You can append --noinputoption to syncdb to shortcut the interactive prompt if you have super user and session info in your initial_data.json
PhilGo20
+7  A: 

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. :-)

Brandon Craig Rhodes
Good stuff! Thanks.
a paid nerd
Finally implemented this and added a hook to create my own test user (if `settings.DEBUG` is `True`) automatically. Thanks again!
a paid nerd
A: 

I'm using sqlite as a dev database. After changing model classes, just drop the corresponding tables with sqlite manager (a firefox plugin, open to inspect the data anyways) and run manage.py syncdb to recreate what's missing.

alex
+3  A: 

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

A Much simpler approach is to, once a super user has been created, to dump the auth module in a json file

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

You can also dumb the session

./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.

Stolen from : http://www.arthurkoziel.com/2008/09/04/automatical-superuser-creation-django/

PhilGo20