Django ImageField is not updating when update() method is used
Asked Answered
C

2

5

I am updating some of fields in model from views.py file. All other fields are updated properly when I use

Profile.objects.filter(id=user_profile.id).update(
        bio=bio,
        city=city,
        date_of_birth=dob,
        profile_pic=profile_pic,
        gender=gender
    )

only, profile_pic = models.ImageField(blank=True) is not updated, Weird thing is when I check my Profile model from admins.py it shows me the file name which I uploaded, But my file is not shown in my /media directory(where I upload my all Images)

views.py

def edit_submit(request):
if request.method == 'POST':
    profile_pic = request.POST.get('profile_pic')
    bio = request.POST.get('bio')
    city = request.POST.get('city')
    dob = request.POST.get('dob')
    gender = request.POST.get('gender')
    user_profile = Profile.objects.get(user=request.user)
    Profile.objects.filter(id=user_profile.id).update(
        bio=bio,
        city=city,
        date_of_birth=dob,
        profile_pic=profile_pic,
        gender=gender
    )
    return HttpResponseRedirect(reverse('profile', args=[user_profile.id]))

This is how I manage my media files in settings.py

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'

I think only text is stored in ImageField and Image is not uploded to /media directory Note: I am using <input type="file" name="profile_pic" class="change_user_img"> for getting image from template

Callow answered 4/1, 2019 at 14:11 Comment(0)
S
11

The QuerySet.update() method doesn't call save() on the model, and so the usual mechanism that places the image into storage is not executed. In addition, you must retrieve the uploaded image from request.FILES not request.POST.

Rather than using update(), if you set the attributes on the model instance and then call save(), the image should get saved to the correct place on disk. For example:

profile_pic = request.FILES.get('profile_pic')  # Use request.FILES

bio = request.POST.get('bio')
city = request.POST.get('city')
dob = request.POST.get('dob')
gender = request.POST.get('gender')

user_profile = Profile.objects.get(user=request.user)
user_profile.bio = bio
user_profile.city = city
user_profile.date_of_birth = dob
user_profile.profile_pic = profile_pic
user_profile.gender = gender
user_profile.save()

You must also ensure that the form has the enctype="multipart/form-data" attribute set.

Stets answered 7/1, 2019 at 11:29 Comment(2)
code same but giving error of AttributeError: 'tuple' object has no attribute '_committed'. Can you look at my question link. my model doesn't have a save method, guess it will be inherited from models.Model. Do we need a QuerySet object for using the save method?Disputant
Hey, I change from .update() to .save() but the problem is still the same. I can see in admin panel that the ImageField has been changed but the photo isnt saving in the directoryRehearing
T
0

Specify upload_to on profile_pic = ...: Docs here.

Teriteria answered 4/1, 2019 at 15:39 Comment(5)
By default I configured my settings.py to store all images in /media dirictory....Callow
How do you have MEDIA_ROOT defined?docs.djangoproject.com/en/2.1/ref/models/fields/…Teriteria
Updated media root in question...@Dan SwainCallow
Is BASE_DIR defined something like the following? PROJECT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(file))) BASE_DIR = os.path.dirname(PROJECT_DIR)Teriteria
And do you have enctype="multipart/form-data" on your <form> tag?Teriteria

© 2022 - 2024 — McMap. All rights reserved.