Skip to content

Instantly share code, notes, and snippets.

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
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