from __future__ import unicode_literals

from django.db import migrations


def create_homepage(apps, schema_editor):
    # Get models
    ContentType = apps.get_model('contenttypes.ContentType')
    Page = apps.get_model('wagtailcore.Page')
    Site = apps.get_model('wagtailcore.Site')
    HomePage = apps.get_model('home.HomePage')

    # Delete the default homepage
    Page.objects.get(id=2).delete()

    # Create content type for homepage model
    homepage_content_type, created = ContentType.objects.get_or_create(
        model='homepage', app_label='home')

    # Create a new homepage
    homepage = HomePage.objects.create(
        title="Homepage",
        slug='home',
        content_type=homepage_content_type,
        path='00010001',
        depth=2,
        numchild=0,
        url_path='/home/',
    )

    # Create a site with the new homepage set as the root
    Site.objects.create(
        hostname='localhost', root_page=homepage, is_default_site=True)


class Migration(migrations.Migration):

    dependencies = [
        ('home', '0001_initial'),
    ]

    operations = [
        migrations.RunPython(create_homepage),
    ]

from __future__ import unicode_literals

from django.db import migrations


def create_homepage(apps, schema_editor):
    # Get models
    ContentType = apps.get_model('contenttypes.ContentType')
    Page = apps.get_model('wagtailcore.Page')
    Site = apps.get_model('wagtailcore.Site')
    HomePage = apps.get_model('home.HomePage')

    # Delete the default homepage
    Page.objects.get(id=2).delete()

    # Create content type for homepage model
    homepage_content_type, created = ContentType.objects.get_or_create(
        model='homepage', app_label='home')

    # Create a new homepage
    homepage = HomePage.objects.create(
        title="Homepage",
        slug='home',
        content_type=homepage_content_type,
        path='00010001',
        depth=2,
        numchild=0,
        url_path='/home/',
    )

    Site = apps.get_model('wagtailcore.Site')
    HomePage = apps.get_model("core", "HomePage")
    homepage = HomePage.objects.get(slug="home")

    Site.objects.filter(hostname='localhost').delete()

    # Create a site with the new homepage set as the root
    Site.objects.create(
        hostname='localhost', root_page_id=homepage.id, is_default_site=True)


class Migration(migrations.Migration):

    dependencies = [
        ('home', '0001_initial'),
    ]

    operations = [
        migrations.RunPython(create_homepage),
    ]

Remove any existing localhost sites and use the page id rather than the object to set the default homepage.
Remove any existing localhost sites and use the page id rather than the object to set the default homepage.

  from __future__ import unicode_literals
  
  from django.db import migrations
  
  
  def create_homepage(apps, schema_editor):
      # Get models
      ContentType = apps.get_model('contenttypes.ContentType')
      Page = apps.get_model('wagtailcore.Page')
      Site = apps.get_model('wagtailcore.Site')
      HomePage = apps.get_model('home.HomePage')
  
      # Delete the default homepage
      Page.objects.get(id=2).delete()
  
      # Create content type for homepage model
      homepage_content_type, created = ContentType.objects.get_or_create(
          model='homepage', app_label='home')
  
      # Create a new homepage
      homepage = HomePage.objects.create(
          title="Homepage",
          slug='home',
          content_type=homepage_content_type,
          path='00010001',
          depth=2,
          numchild=0,
          url_path='/home/',
      )
  
+     Site = apps.get_model('wagtailcore.Site')
+     HomePage = apps.get_model("core", "HomePage")
+     homepage = HomePage.objects.get(slug="home")
+ 
+     Site.objects.filter(hostname='localhost').delete()
+ 
      # Create a site with the new homepage set as the root
      Site.objects.create(
-         hostname='localhost', root_page=homepage, is_default_site=True)
+         hostname='localhost', root_page_id=homepage.id, is_default_site=True)
  
  
  class Migration(migrations.Migration):
  
      dependencies = [
          ('home', '0001_initial'),
      ]
  
      operations = [
          migrations.RunPython(create_homepage),
      ]
  
Remove any existing localhost sites and use the page id rather than the object to set the default homepage.
## Code Before:
from __future__ import unicode_literals

from django.db import migrations


def create_homepage(apps, schema_editor):
    # Get models
    ContentType = apps.get_model('contenttypes.ContentType')
    Page = apps.get_model('wagtailcore.Page')
    Site = apps.get_model('wagtailcore.Site')
    HomePage = apps.get_model('home.HomePage')

    # Delete the default homepage
    Page.objects.get(id=2).delete()

    # Create content type for homepage model
    homepage_content_type, created = ContentType.objects.get_or_create(
        model='homepage', app_label='home')

    # Create a new homepage
    homepage = HomePage.objects.create(
        title="Homepage",
        slug='home',
        content_type=homepage_content_type,
        path='00010001',
        depth=2,
        numchild=0,
        url_path='/home/',
    )

    # Create a site with the new homepage set as the root
    Site.objects.create(
        hostname='localhost', root_page=homepage, is_default_site=True)


class Migration(migrations.Migration):

    dependencies = [
        ('home', '0001_initial'),
    ]

    operations = [
        migrations.RunPython(create_homepage),
    ]

## Instruction:
Remove any existing localhost sites and use the page id rather than the object to set the default homepage.
## Code After:
from __future__ import unicode_literals

from django.db import migrations


def create_homepage(apps, schema_editor):
    # Get models
    ContentType = apps.get_model('contenttypes.ContentType')
    Page = apps.get_model('wagtailcore.Page')
    Site = apps.get_model('wagtailcore.Site')
    HomePage = apps.get_model('home.HomePage')

    # Delete the default homepage
    Page.objects.get(id=2).delete()

    # Create content type for homepage model
    homepage_content_type, created = ContentType.objects.get_or_create(
        model='homepage', app_label='home')

    # Create a new homepage
    homepage = HomePage.objects.create(
        title="Homepage",
        slug='home',
        content_type=homepage_content_type,
        path='00010001',
        depth=2,
        numchild=0,
        url_path='/home/',
    )

    Site = apps.get_model('wagtailcore.Site')
    HomePage = apps.get_model("core", "HomePage")
    homepage = HomePage.objects.get(slug="home")

    Site.objects.filter(hostname='localhost').delete()

    # Create a site with the new homepage set as the root
    Site.objects.create(
        hostname='localhost', root_page_id=homepage.id, is_default_site=True)


class Migration(migrations.Migration):

    dependencies = [
        ('home', '0001_initial'),
    ]

    operations = [
        migrations.RunPython(create_homepage),
    ]
