ValueError for FileField if the file is closed in clean method
|Reported by:||Owned by:||nobody|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
Say you have a Model with a FileField, and you need to override clean to do some validation on the file before it is saved. If the file is closed within clean and the file is over 2.5MB (or larger than FILE_UPLOAD_MAX_MEMORY_SIZE) a "ValueError: I/O operation on closed file" will result. If you attempt to re-open the file in clean to avoid this error a different ValueError will result stating "The file cannot be reopened." This is particularly a problem if the verification of a file uses some other library that may open and close the file.
I'm using Django 1.4.3, and have seen the issue under the Django web development server as well as under Apache w/mod_wsgi. Here's an example models.py to illustrate the issue:
from string import join from django.db import models from FileUploadExample.settings import MEDIA_ROOT def some_file_operation(file): file.open() file.close() def upload_file_path(instance, filename): upload_dir = join([MEDIA_ROOT, str(filename)], '') return upload_dir class UploadedFile(models.Model): uploaded_file = models.FileField(upload_to=upload_file_path) def clean(self): # some call to somewhere to validate the file some_file_operation(self.uploaded_file)