How to use '[' or ']' for the file name of upload_to in Django filefield

28 views Asked by At

I'm trying to put the file name that includes '[' and ']' in the Django model.

Examples include:

[Test] Test.pdf

However, the actual locally saved file names are as follows:

Test.pdf

Problems occur when importing files via file name because '[' and ']' are saved as removed.

models.py:

def evidence_directory_path(instance, filename):
    return f'{instance.user_uuid}/Evidence/{slugify(instance.product)}/{filename}'

class Evidence(models.Model):
    user_uuid = models.CharField(max_length=50)
    title = models.CharField(max_length=200)
    product = models.CharField(max_length=500)
    uploadedFile = models.FileField(upload_to=evidence_directory_path)
    dateTimeOfUpload = models.DateTimeField(auto_now=True)

The output of the following functions is as follows:


def evidence_directory_path(instance, filename):
    print(filename)
    return f'{instance.user_uuid}/Evidence/{slugify(instance.product)}/{filename}'

[Test]Test.pdf

However, the actual file name saved is as follows:

TestTest.pdf

What kind of mistake am I making?

I have no idea.

0

There are 0 answers