I have model UserProfile with field avatar = models.ImageField(upload_to=upload_avatar)
upload_avatar function names image file according user.id (12.png for example).
But when user updates the avatar, new avatar name coincide with old avatar name and Django adds suffix to file name (12-1.png for example).
There are way to overwrite file instead of create new file?
Yeah, this has come up for me, too. Here’s what I’ve done.
Model:
Also defined in models.py:
In a separate file, storage.py:
Obviously, these are sample values here, but overall this works well for me and this should be pretty straightforward to modify as necessary.