# changed file upload is not getting saved

**URL:** <https://forum.djangoproject.com/t/changed-file-upload-is-not-getting-saved/12004>\
**Category:** Forms & APIs\
**Created:** [February 7, 2022, 1:51am UTC](https://forum.djangoproject.com/t/changed-file-upload-is-not-getting-saved/12004 "2022-02-07T01:51:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![gunaratna](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/gunaratna/32/1900_2.png) [@gunaratna](https://forum.djangoproject.com/u/gunaratna)\
**Post date:** [February 7, 2022, 1:51am UTC](https://forum.djangoproject.com/t/changed-file-upload-is-not-getting-saved/12004/1 "2022-02-07T01:51:00Z")

</div>

## I have uploaded a file earlier. when I want to change that using new file, the new file is not getting saved. please, help.

Here the code in view.py

```auto
def profile2(request):
    # if this is a POST request we need to process the form data
    
        # create a class instance and populate it with data from the request:
    if request.method == 'POST' and 'id_document' in request.FILES:
        form = ProfileForm(request.POST,request.FILES)
        if form.is_valid():
            cd = form.cleaned_data
            pf = profile(
                uname = request.user,
                first_name = cd['first_name'],
                last_name = cd['last_name'],
                birth_date = cd['birth_date'],
                Country = cd['Country'],
                eMail= cd['eMail'],
                id_document = cd['id_document'],
                
                
            )
            pf.registration = False
            pf.save()

     
        return HttpResponseRedirect(reverse('index'))

```

Here is forms.py

```auto
class ProfileForm(ModelForm):
    class Meta:
        model = profile
        fields = ['first_name', 'last_name', 'birth_date', 'Country', 'eMail','id_document']
        widgets = {
            'birth_date': DateInput(attrs={'type': 'date'})
        }

```

 ![sampleForm](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/2X/2/21ddbac1243ffc54ec2db5c40bb48e4220c81239.jpeg)

---

<div class="post-metadata">

**Author:** ![CodenameTim](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/codenametim/32/31507_2.png) [@CodenameTim](https://forum.djangoproject.com/u/CodenameTim)\
**Post date:** [February 7, 2022, 7:31pm UTC](https://forum.djangoproject.com/t/changed-file-upload-is-not-getting-saved/12004/2 "2022-02-07T19:31:54Z")

</div>

> [@gunaratna](#):
>
> ```auto
> pf = profile(
> uname = request.user,
> first_name = cd['first_name'],
> last_name = cd['last_name'],
> birth_date = cd['birth_date'],
> Country = cd['Country'],
> eMail= cd['eMail'],
> id_document = cd['id_document'],
>                 
>                 
> )
> pf.registration = False
> pf.save()
> 
> ```

This appears to be creating a new instance of `profile`. I believe you would want to look up if the user has an existing profile first. Update if it exists otherwise create a new one. You could supply the instance to the form or you could do it directly like you are with [`update_or_create`](https://docs.djangoproject.com/en/4.0/ref/models/querysets/#update-or-create)

---

<div class="post-metadata">

**Author:** ![gunaratna](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/gunaratna/32/1900_2.png) [@gunaratna](https://forum.djangoproject.com/u/gunaratna)\
**Post date:** [February 7, 2022, 7:55pm UTC](https://forum.djangoproject.com/t/changed-file-upload-is-not-getting-saved/12004/3 "2022-02-07T19:55:38Z")

</div>

Thank you.  
I changed the code. However, it is still not ‘saving’ the changes.

```auto
if request.method == 'POST' and 'id_document' in request.FILES:
        form = ProfileForm(request.POST,request.FILES)
        if form.is_valid():
            cd = form.cleaned_data
            pf = profile.objects.update_or_create(
                uname = request.user,
                first_name = cd['first_name'],
                last_name = cd['last_name'],
                birth_date = cd['birth_date'],
                Country = cd['Country'],
                eMail= cd['eMail'],
                id_document = cd['id_document'],
                
                
            )
            pf.registration = False
            pf.save()

     
        return HttpResponseRedirect(reverse('index'))

```

---

<div class="post-metadata">

**Author:** ![CodenameTim](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/codenametim/32/31507_2.png) [@CodenameTim](https://forum.djangoproject.com/u/CodenameTim)\
**Post date:** [February 7, 2022, 8:11pm UTC](https://forum.djangoproject.com/t/changed-file-upload-is-not-getting-saved/12004/4 "2022-02-07T20:11:20Z")

</div>

I think you may not understand `update_or_create` well enough. You should be supplying the `defaults` argument where most of the values would be set. Any other keyword argument should be the fields that will generate a unique instance. In your case, I believe it should be the user reference, `uname`.

> [@gunaratna](#):
>
> However, it is still not ‘saving’ the changes.

Are you sure the form is returning `True` for `form.is_valid()`? It looks like regardless of that call, a redirect response is returned.

---

<div class="post-metadata">

**Author:** ![gunaratna](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/gunaratna/32/1900_2.png) [@gunaratna](https://forum.djangoproject.com/u/gunaratna)\
**Post date:** [February 8, 2022, 1:37am UTC](https://forum.djangoproject.com/t/changed-file-upload-is-not-getting-saved/12004/5 "2022-02-08T01:37:11Z")

</div>

Thanks again for answer.  
form.is\_valid() is ‘False’. I am not sure what is happening here.
