[Django]-Django: Lowercasing filename of the input Image File

7👍

FileField.upload_to may also be a callable, per this comment in the documentation:

This may also be a callable, such as a function, which will be called to obtain the upload path, including the filename. This callable must be able to accept two arguments, and return a Unix-style path (with forward slashes) to be passed along to the storage system. The two arguments that will be passed are:

Since ImageField inherits all its attributes and methods from FileField, I think you could use:

def update_filename(instance, filename):
    path_you_want_to_upload_to = "img"
    return os.path.join(path_you_want_to_upload_to, filename.lower())

class Foo(models.Model):
    poster = models.ImageField(u"Poster", upload_to=update_filename)

Leave a comment