Saving Original File Name in Django with FileField

django, filenames, upload

Solution

Do that in the view (after null=True, blank=True are again part of your model):

file_object = UploadFileForm.save(commit=False)
file_object.original_filename = request.FILES['file'].name
file_object.save()

Mind that you will need to change the above code accordingly with your context etc

Problem

``` 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) ``` On upload, ``` {"errors": {"original_filename": ["This field is required."]}, "success": false} ``` Adding blank=True, null=True to the FileUpload.original_filename allows the upload to succeed but does not save the original file name. On Django 1.5. According to this post, this should work.

Original source

Related problems