[Answered ]-Django file not uploaded using FileField(upload to=)

1👍

The folder will remain empty since you made a script that generates a new directory, but never do you ask to save that file there.

However I think you are solving the problem at the wrong location. The form does not need to handle that. A Django model can handle this: you can pass a function as value for the upload_to=… parameter [Django-doc]:

class UploadedFiles(models.Model):
    def file_path(self, filename):
        return f'uploaded_files/{self.server}/{self.path.replace(":","$", 1)}/{filename}'
        
    files_to_upload = models.FileField(
        upload_to=file_path,
        default=None,
        validators=[validate_file]
    )
    path = models.CharField(max_length=100)
    server = MultiSelectField(choices=server_list)

    SalesforceTicket = models.ForeignKey(
        SalesforceTicket,
        related_name='files',
        on_delete=models.CASCADE
    )

    def __str__(self):
        return str(self.SalesforceTicket)

or something similar.

Leave a comment