Django: Lower case input file name

I have a model:

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

I use admin to place posters and save objects Foo. Now I need to find a way to change the file name before saving. For example, POSTER.png or Poster.png or a poster .PNG should be reduced to poster.png.

What would be the easiest way to achieve this?

+3
source share
1 answer

FileField.upload_to can also be called for this comment in the documentation:

, , , , . Unix ( ), . , , :

ImageField FileField, , :

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)
+6

Source: https://habr.com/ru/post/1759078/


All Articles