1👍
✅
The .update()
method works at the database level and will ignore upload_to
. It may also overwrite other users’ avatars if the same filename is used when it should actually store the file using a unique filename.
The correct way is to use a proper form to update data based on user input, but if you are in a hurry this should also work:
try:
profile = Profile.objects.get(user_id=request.POST['user_id'])
profile.avatar = request.FILES['avatar']
profile.save()
except Profile.DoesNotFound:
return HttpResponseNotFound("User not found")
Source:stackexchange.com