Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@satomacoto
Created May 12, 2019 23:48
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 satomacoto/41eae96ec93c5a6eddc3633c5df1f121 to your computer and use it in GitHub Desktop.
Save satomacoto/41eae96ec93c5a6eddc3633c5df1f121 to your computer and use it in GitHub Desktop.
copy django FileFiled object to tempfile
import shutil
import tempfile
from app.models import Record
# %%
%%time
with tempfile.NamedTemporaryFile() as tmp:
record = Record.objects.first()
tmp.write(record.datafile.read())
# %%
%%time
with tempfile.NamedTemporaryFile() as tmp:
record = Record.objects.first()
with record.datafile.open() as src:
shutil.copyfileobj(src, tmp)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment