Skip to content

Instantly share code, notes, and snippets.

@melvinkcx
Created September 11, 2018 14:05
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
Reducing Memory Footprint When Creating Archive in Django
from django.core.files.base import ContentFile
from django.db import models
file_storage = FileSystemStorage(location='/archive')
class MyFileModel(models.Model):
"""
A sample model
"""
archive = models.FileField(storage=file_storage)
@classmethod
def upload(cls, file):
my_file_model = cls()
my_file_model.archive.save(
file.name,
ContentFile(file.read())
)
import tempfile
import zipfile
from django.core.files import File
temp = tempfile.NamedTemporaryFile() # Django cannot read TempFile
with zipfile.ZipFile(temp, mode='w', compression=zipfile.ZIP_DEFLATED) as archive:
# Write something into archive
# Eg: archive.writestr('{}'.format(filename), file_content)
pass
# Assume upload() is one of the methods in our Model
with transaction.atomic():
temp.seek(0) # Move the cursor back to the begining of the file
file = File(temp) # Wrap NamedTempFile with DjangoFile
file.name = 'Some File Name.zip'
MyFileModel.upload(file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment