Skip to content

Instantly share code, notes, and snippets.

@camilonova
Last active March 28, 2018 22:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save camilonova/3bdb55ba28560a50748665d34019e29a to your computer and use it in GitHub Desktop.
Save camilonova/3bdb55ba28560a50748665d34019e29a to your computer and use it in GitHub Desktop.
Migrate a django file field to a S3 field
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