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.
- easy_install south
- 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...
Related videos on Youtube
a paid nerd
I'm a professional programmer. I also like riding bicycles. I haven't seen the ResEdit clown in a long time -- or in as anachronistic an environment ever. -- @ruffin 8bde9408bf59382c3194ec7c1bf6afbd837875d7
Updated on September 06, 2020Comments
-
a paid nerd almost 4 years
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
?-
Sdra over 7 years
syncdb
has been deprecated in favor of data migrations
-
-
a paid nerd over 14 yearsFinally implemented this and added a hook to create my own test user (if
settings.DEBUG
isTrue
) automatically. Thanks again! -
philgo20 over 14 yearsYou can append --noinput option to syncdb to shortcut the interactive prompt if you have super user and session info in your initial_data.json
-
ropable almost 14 yearsI like this answer. Thanks for the suggestion!
-
TimP about 11 years
if 'syncdb' in sys.argv: sys.argv.append('--noinput')
-
Andrei-Niculae Petre about 11 yearsme too, thanks! clean solution, already using it to have a clean dump which I syncdb whenever I need.
-
bnjmn almost 11 yearsThis should really be the accepted answer. Most straightforward IMO. Link is broken. :(
-
tjb over 10 yearsreset has been replaced by flush with Django 1.5 stackoverflow.com/questions/15454008/…
-
user272735 over 9 yearsWhere I should put
initial_data.json
so thatsyncdb
finds it ? The docs says: "In the fixtures directory of every installed application". Is that e.g../eggs/Django-1.6.5-py2.7.egg/django/contrib/auth/fixtures
? -
nealmcb over 9 yearsThanks! This is clearer than the others, more robust and extensible, and great for use the first time you run your code, as well as in test scripts and certain deployment scenarios, and of course for the development scenario that led to the question.
-
Germain Chazot almost 9 yearsThis is deprecated since Django 1.7: docs.djangoproject.com/en/1.7/howto/initial-data/… Now you can use Data migrations.
-
Adrian Petrescu over 8 yearsUnfortunately, this no longer works as of Django 1.9, because
django.contrib.auth
is no longer available at configuration time. This is by design and has been deprecated since 1.8 so it's unlikely to ever come back. Which is very sad... I liked this hack. -
Adrian Petrescu over 8 yearsOkay, I figured out how to fix your code to work with Django 1.9! I've edited your answer with the fixes. Thanks for posting it :)
-
Erion S almost 8 yearsIf it was created by you then please add a disclaimer
-
Dunatotatos about 6 yearsIs it compatible with Django >=2.0 ?
-
un33k about 6 years@Dunatotatos yes it is. Django version support can also be found in the .travis.yml file of the repo