def generate_uuid_file_name(self, filename):
self.original_filename = filename
extension = filename.rsplit('.', 1)[1]
newfilename = uuid.uuid4().__str__() + '.' + extension
return self.directory() + newfilename
class FileUpload(models.Model):
original_filename = models.CharField(max_length=128)
fileobj = models.FileField(upload_to=generate_uuid_file_name)
上传时,
{"errors": {"original_filename": ["This field is required."]}, "success": false}
将File = true,null = True添加到FileUpload.original_filename允许上传成功但不保存原始文件名。在Django 1.5上。根据{{3}},这应该有用。
答案 0 :(得分:6)
在视图中执行此操作(在null = True之后,blank = True再次成为模型的一部分):
file_object = UploadFileForm.save(commit=False)
file_object.original_filename = request.FILES['file'].name
file_object.save()
请注意,您需要根据上下文等相应地更改上述代码