Creating a Django Admin Account on Elastic Beanstalk

From Littledamien Wiki
Jump to navigation Jump to search


Create a custom Django command.

Custom Django commands are placed in a package named management.commands in an app separate from the main app, e.g. project_root/not_main_app/management/commands/my_custom_command.py. Django can locate the commands automatically when running manage.py when they follow this convention.

# not_main_app/management/commands/create_su.py
import os

from django.core.management.base import BaseCommand

from com.cygora.apps.users.models.User import User

class Command(BaseCommand):

    def handle(self, *args, **options):
        username = os.environ['SU_USERNAME']
        if not User.objects.filter(username=username).exists():
            User.objects.create_superuser(username,
                                          os.environ['SU_USERNAME'],
                                          os.environ['SU_PASSWORD'])

Note that it's essential to place __init__.py files in each of the directories all the way down to the commands directory.

I created a package solely for the purpose of hosting this management command named eb_deploy.

Add a containers command to a .ebextensions .config file. (This should come after migration commands.):

container_commands:
  02_create_superuser_for_django_admin:
    command: "python manage.py create_su"
    leader_only: true

Notes[edit]