I’m trying to create a field in a model, that should store an image for a registered user.
This image should be renamed and stored in a separate user directory like media/users/10/photo.jpeg.
I’ve searched a lot, but still can’t find how to do it cleanly and correctly. It seems to me that many sites require the same functionality and this should be in django docs, but it is not.
You want to use the “upload_to” option on an
ImageFieldThis is code directly from a project of mine. The uploaded image goes to
/MEDIA_ROOT/photos/<user_id>/filenameFor what you want, just change the ‘photos’ string to ‘users’ in
def get_image_pathHere is the small bit about it in the docs, under FileField details