# -*- coding: utf-8 -*-
# Generated by Django 1.11.4 on 2017-08-02 22:46
from __future__ import unicode_literals

from django.db import migrations, models


def copy_pagebase_id(apps, schema_editor):
    current_database = schema_editor.connection.alias
    QuestionPage = apps.get_model('wizard_builder.QuestionPage')
    Page = apps.get_model('wizard_builder.Page')
    for question_page in QuestionPage.objects.using(current_database):
        with schema_editor.connection.cursor() as cursor:
            cursor.execute(
                'INSERT INTO "wizard_builder_page" ("id", "position", "section", "infobox") VALUES (%s, %s, %s, %s)', [
                    question_page.id, question_page.position, question_page.section, question_page.infobox, ], )
        page = Page.objects.get(id=question_page.id)
        for site in question_page.sites.all():
            page.sites.add(site)


class Migration(migrations.Migration):

    dependencies = [
        ('wizard_builder', '0012_questionpage_to_page'),
    ]

    operations = [
        migrations.RunPython(
            copy_pagebase_id,
            reverse_code=migrations.RunPython.noop,
        ),
    ]

@@ -316,57 +316,8 @@
 e')%0A
-    Page = apps.get_model('wizard_builder.Page')%0A
     
@@ -695,18 +695,16 @@
 .infobox
-, 
 %5D, )%0A   
@@ -712,131 +712,239 @@
     
-page = Page.objects.get(id=question_page.id)%0A        for site in question
+    for site in question_page.sites.all():%0A                cursor.execute(%0A                    'INSERT INTO %22wizard_builder
 _page
-.
+_
 sites
-.all():%0A            page.sites.add(site
+%22 (%22page_id%22, %22site_id%22) VALUES (%25s, %25s)', %5B%0A                        question_page.id, site.id%5D, 
 )%0A%0A%0A

use cursor for all writes in this migration