Created
          May 12, 2019 23:48 
        
      - 
      
- 
        Save satomacoto/41eae96ec93c5a6eddc3633c5df1f121 to your computer and use it in GitHub Desktop. 
    copy django FileFiled object to tempfile
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | 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