Skip to content

Instantly share code, notes, and snippets.

@melvinkcx
Created Sep 11, 2018
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