Skip to content

Instantly share code, notes, and snippets.

Created Sep 11, 2018
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)
def upload(cls, file):
my_file_model = cls(),
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)
# Assume upload() is one of the methods in our Model
with transaction.atomic(): # Move the cursor back to the begining of the file
file = File(temp) # Wrap NamedTempFile with DjangoFile = 'Some File'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment