Last active
March 28, 2018 22:06
-
-
Save camilonova/3bdb55ba28560a50748665d34019e29a to your computer and use it in GitHub Desktop.
Migrate a django file field to a S3 field
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.core.files.base import ContentFile | |
qs = Category.objects.exclude(image='').exclude(image_s3__isnull=False) | |
count = 0 | |
total = qs.count() | |
for obj in qs: | |
new_file = ContentFile(obj.image.file.read()) | |
new_file.name = obj.image.name.split('/')[-1:][0] | |
obj.image_s3 = new_file | |
obj.save() | |
obj.image.file.close() | |
count += 1 | |
print('< ', obj.image.url) | |
print('> ', obj.image_s3.url) | |
print('{}/{}\n'.format(count, total)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment