[Django]-How does Django rename uploaded files?

6👍

Django default Storage class method called get_available_name

    # If the filename already exists, add an underscore and a random 7
    # character alphanumeric string (before the file extension, if one
    # exists) to the filename until the generated filename doesn't exist.
    # Truncate original name if required, so the new filename does not
    # exceed the max_length.

Django by default saves object by its name but if object with that name already exists adds up underscore and 7 random chars as quoted in code comment

Also as addition to this Django Storage class method get_valid_name parses up file name before and replaces all spaces with underscores and removes all chars that are not unicode, alpha, dash, underscore or dot

re.sub(r'(?u)[^-\w.]', '', s)

Leave a comment