I have read through all the documentation for TemporaryUploadedFiles and InMemoryUploadedFiles but they talk never about clean up. I know that Python Temporary Files need to be closed in order to be deleted automatically.
@api_view(['POST', ])
def test(request):
#img is type InMemoryUploadedFile
for img in request.FILES.getlist('images'):
Model.objects.image = img
Model.save()
#vid is type TemporaryUploadedFile
for vid in request.FILES.getlist('videos'):
Model.objects.video = vid
Model.save()
In the code above, are the TemporaryUploadedFiles and InMemoryUploadedFiles deleted and cleaned up automatically?
These files are closed, WSGI handler can provide a method called close and that method is called post-processing of the request.
Django source code (http/request.py)
class HttpRequest:
...
def close(self):
if hasattr(self, '_files'):
for f in chain.from_iterable(list_[1] for list_ in self._files.lists()):
f.close()
Edit:
Django globally configures two types of file uploader InMemory and Temp file. All these classes implement theclose method, the close method of request object is registered as _resource_closers and these methods are called from HttpResponseBase's close method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With