[Answer]-Optimize a South data migration for a huge number of objects

1👍

You can use django’s Paginator to split the queryset into pages/chunks:

paginator = Paginator(MediaFile.objects.all(), 10)
for i in xrange(1, paginator.num_pages):
    for mediafile in paginator.page(i).object_list:
        try:
            mediafile.photo.mediafileobject = mediafile
        except AttributeError:
            pass # When mediafile.photo is None
        else:
            mediafile.photo.save()

Hope that helps.

👤alecxe

Leave a comment