57👍
✅
You want to use the “upload_to” option on an ImageField
#models.py
import os
def get_image_path(instance, filename):
return os.path.join('photos', str(instance.id), filename)
class UserProfile(models.Model):
user = models.ForeignKey(User, unique=True)
profile_image = ImageField(upload_to=get_image_path, blank=True, null=True)
This is code directly from a project of mine. The uploaded image goes to /MEDIA_ROOT/photos/<user_id>/filename
For what you want, just change the ‘photos’ string to ‘users’ in def get_image_path
Here is the small bit about it in the docs, under FileField details
2👍
I suggest you to look into django-photologue. Its a django app with all image managment, uploading and storing already done!
More about at: http://code.google.com/p/django-photologue/
- [Django]-What is the benefit of using a HyperlinkedModelSerializer in DRF?
- [Django]-Django.db.migrations.exceptions.InconsistentMigrationHistory
- [Django]-How do I go straight to template, in Django's urls.py?
1👍
You’ll need to make a model that has a foreign key to the User model, where you can have an image field for the User. I would not recommend modifying the User model itself.
- [Django]-Is it better to use path() or url() in urls.py for django 2.0?
- [Django]-Has Django served an excess of 100k daily visits?
- [Django]-Manually trigger Django email error report
Source:stackexchange.com