Estaba teniendo el mismo problema y pude resolverlo en mi caso. Este es el código que Django usa para determinar el tamaño de un archivo:
def _get_size(self):
if not hasattr(self, '_size'):
if hasattr(self.file, 'size'):
self._size = self.file.size
elif os.path.exists(self.file.name):
self._size = os.path.getsize(self.file.name)
else:
raise AttributeError("Unable to determine the file's size.")
return self._size
Por lo tanto, Django plantear una AttributeError
si el archivo no existe en el disco (o tener un atributo de tamaño ya definido). Dado que la clase TemporaryFile
intenta crear un archivo en la memoria en lugar de realmente en el disco, este método _get_size
no funciona. Con el fin de conseguir que funcione, que tenía que hacer algo como esto:
import tempfile, os
# Use tempfile.mkstemp, since it will actually create the file on disk.
(temp_filedescriptor, temp_filepath) = tempfile.mkstemp()
# Close the open file using the file descriptor, since file objects
# returned by os.fdopen don't work, either
os.close(temp_filedescriptor)
# Open the file on disk
temp_file = open(temp_filepath, "w+b")
# Do operations on your file here . . .
modelObj.fileField.save("filename.txt", File(temp_file))
temp_file.close()
# Remove the created file from disk.
os.remove(temp_filepath)
Alternativamente (y preferiblemente), si se puede calcular el tamaño del archivo temporal que está creando, podría configurar un atributo de tamaño en el objeto TemporaryFile
directamente. Debido a las bibliotecas que estaba usando, esta no era una posibilidad para mí.
¿Cuál es el tipo de error? –
@john Es un AttributeError. – Conan