Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 ?

like image 791
a paid nerd Avatar asked Sep 23 '09 15:09

a paid nerd


People also ask

How do I make administrative superuser in Django?

Django's “Hello World” application Start the terminal by clicking on the “Run” button. Type python3 manage.py createsuperuser in the given terminal and press “Enter”. The system will ask for credentials, after which a superuser will be created. To run the server, we type the command python3 manage.py runserver 0.0.

What is the difference between Django admin and manage py?

Django-admin.py: It is a Django's command line utility for administrative tasks. Manage.py: It is an automatically created file in each Django project. It is a thin wrapper around the Django-admin.py.

What does Python manage py Syncdb do?

syncdb is a command which is executed in django shell to create tables for first time for apps which are added to INSTALLED_APPS of settings.py.

What does Django admin Startapp do?

The startproject will create the main project directory, while the startapp will create the app directory. Both are also been passed a name to be used in generation. The startproject is the first command run when creating a new project, while the startapp is run inside the new project directory.


2 Answers

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.

like image 55
philgo20 Avatar answered Oct 13 '22 15:10

philgo20


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 
like image 20
Andre Miller Avatar answered Oct 13 '22 14:10

Andre Miller