我有一个名为Upload2的Django2模型,它只有一个FileField(将包含一个excel文件)。
通过管理表单上传时,我需要对上传的excel文件的行/列执行一些检查,并在出现错误的情况下抛出ValidationError。
问题是:由于似乎文件尚未写入文件系统,因此如何在clean()方法中访问文件?还是在其他地方写过?还是可以读取字节流并将其写入临时文件?
class Upload(models.Model):
file_field = models.FileField(upload_to='files/')
def clean(self):
filename = str(self.file_field.path)
print('uploaded file: ' + filename)
# ISSUE: self.file_field.path is not (yet) existing on file system, where is the file?
# is there a way to access the content of uploaded file?
perform_check_on_excel_file(self.file_field.path) # file not found