[Fixed]-Django File Upload and Rename


You just need to change your content_file_name function. The function below will create paths like so: uploads/42_100.c, where 42 is the user’s id, and 100 is the question’s id.

import os
def content_file_name(instance, filename):
    ext = filename.split('.')[-1]
    filename = "%s_%s.%s" % (instance.user.id, instance.questid.id, ext)
    return os.path.join('uploads', filename)

Leave a comment