Question

As part of an automation process I need to create the superuser for Django via python in the CLI.

I could consider other possibilities to this problem but only if they're easily scritable with ansible.

Anyway, this is what I have so far (for Django 1.6)

python -c "import os; 
  os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'my_proj.live_settings'); 
  from django.conf import settings;"

python -c "from django.contrib.auth import get_user_model; 
  user_model = get_user_model();
  user_model.objects.db_manager().create_superuser('admin', 'admin@admin.com', 'ad_pass')"

However, when I run this i get this error.

django.core.exceptions.ImproperlyConfigured: Requested setting CACHES, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

Any idea what's wrong or how to fix it please? It seems strange it should complain about DJANGO_SETTINGS_MODULE when it has been explicitly set..

Was it helpful?

Solution

The problem you're seeing is because you're running it as two separate python commands. You import the settings in the first, then python exits, then you start the second (which hasn't imported settings).

If you put them all into one python -c call, it should work.

It also looks like you're not passing the right number of arguments to .db_manager(). See this for more details:

https://docs.djangoproject.com/en/1.6/topics/db/multi-db/#using-managers-with-multiple-databases

OTHER TIPS

How about adding a custom manage.py command?

[appname]/managment/commands/add_user.py:

from django.core.management.base import BaseCommand
from django.contrib.auth.models import User
from optparse import make_option

class Command(BaseCommand):
    option_list = BaseCommand.option_list + (
        make_option('--user',
            action='store',
            default=None,
            help='Username for new user'),
        make_option('--password',
            action='store',
            default=None,
            help='User Password'),
        make_option('--email',
            action='store',
            default=None,
            help='User Email Address'),
        )

    def handle(self, *args, **kwargs):
        user = User.objects.create_user(
            username = kwargs.get('user'),
            password = kwargs.get('password'),
            email = kwargs.get('email')
            )
        user.save( )               

add a user with the following command from the CLI:

python manage.py add_user --user=tester --password=tester --email=tester@test.org

With the code that shtuff.it specified, work successfully and create the user but I cannot login to the created user, so I need to modify the code to create the superuser.

P.S: I have changed the order of parameter, like I have put the email before the password and it work for me.

Here is the complete code that we can use to create the django superuser from CLI:

[appname]/managment/commands/add_user.py:

from django.core.management.base import BaseCommand
from django.contrib.auth.models import User
from optparse import make_option

class Command(BaseCommand):
    option_list = BaseCommand.option_list + (
        make_option('--user',
            action='store',
            default=None,
            help='Username for new user'),
        make_option('--password',
            action='store',
            default=None,
            help='User Password'),
        make_option('--email',
            action='store',
            default=None,
            help='User Email Address'),
        )

    def handle(self, *args, **kwargs):
        user = User.objects.create_superuser(
            username = kwargs.get('user'),
            email = kwargs.get('email'),
            password = kwargs.get('password').strip(),
            )
        user.save( )

Then use the following command to add the user from CLI:

python manage.py add_user --user=USERNAME --password=PASSWORD --email=mail@test.com

For my needs I have created little app that creates superuser by using fixtures or migrations. https://github.com/inirudebwoy/django-mksuperuser

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top