Programatically setting up Django with Wagtail

Intro

Okay so you have installed wagtail and you are starting from scratch with a drop database/create database and you have just run manage.py migrate. After that you need to go through the pain of creating a superuser, logging in setting up the site root page etc etc. In reality I just want to have a script I can run that can set up everything that I need to develop the site.

Here is a management command that I run to get going:

from django.core.management.base import BaseCommand
from wagtail.core.models import Page, Site

from home.models import HomePage
from django.contrib.auth import get_user_model


User = get_user_model()


class Command(BaseCommand):
    help = "Gets the site up and running for Devs"

    def handle(self, *args, **options):

        # create site
        Page.objects.exclude(title="Root").delete()
        parent = Page.objects.get(url_path="/")
        home_page = HomePage(title="home", body="home", slug="home")
        parent.add_child(instance=home_page)
        Site.objects.all().delete()
        Site.objects.create(
            hostname="test.com", is_default_site=True, root_page=home_page
        )
        User.objects.create_superuser("admin", "admin@admin.com", "admin")

Let me know if this helps on twitter @chriswedgwood. As your site grows in complexity this will grow