from django.db import migrations

def migrate_images(apps, schema_editor):
    Post = apps.get_model('posts', 'Post')
    PostImage = apps.get_model('posts', 'PostImage')

    for post in Post.objects.all():
        for index, image in enumerate(post.old_images.all()):
            PostImage.objects.create(post=post, image=image, order=index)

def reverse_migrate_images(apps, schema_editor):
    PostImage = apps.get_model('posts', 'PostImage')
    PostImage.objects.all().delete()

class Migration(migrations.Migration):

    dependencies = [
        ('posts', '0007_postimage_post_images'),
    ]

    operations = [
        migrations.RunPython(migrate_images, reverse_migrate_images),
    ]
