# How do I upload a file in Django OneToOne Model's FileField using form?

**URL:** <https://forum.djangoproject.com/t/how-do-i-upload-a-file-in-django-onetoone-models-filefield-using-form/12996>\
**Category:** Forms & APIs\
**Created:** [April 4, 2022, 1:14pm UTC](https://forum.djangoproject.com/t/how-do-i-upload-a-file-in-django-onetoone-models-filefield-using-form/12996 "2022-04-04T13:14:33Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![khaw235](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/khaw235/32/6372_2.png) [@khaw235](https://forum.djangoproject.com/u/khaw235)\
**Post date:** [April 4, 2022, 1:14pm UTC](https://forum.djangoproject.com/t/how-do-i-upload-a-file-in-django-onetoone-models-filefield-using-form/12996/1 "2022-04-04T13:14:33Z")

</div>

Hi there,

I have one model `Detail` which has `OneToOne` relation with default `User` Model. I have a field `FileField` in my `Detail` model, where I want to upload the files using forms from frontend/templates.

I have been working around it a lot but I am not getting it done. I need help, please.

My **`models.py`** is:

```auto
from django.db import models
from django.contrib.auth.models import User

class Detail(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    file = models.FileField(verbose_name="CSV File", upload_to='csv_files')
    file_desc = models.TextField("CSV File Description")

    def __str__ (self):
        return ("{} ({} {})".format(self.user.email, self.user.first_name, self.user.last_name))

```

My **`forms.py`** is:

```auto
from django.forms import ModelForm
from .models import Detail

class DetailForm(ModelForm):
    class Meta:
        model = Detail
        fields = ['file', 'file_desc']

```

My **`views.py`** is:

```auto
from django.views import View

class UserAPI(View):
    template_name = 'accounts/user.html'

    def get(self, request):
        form = DetailForm(instance=request.user)

        context = {'form': form}
        return render(request, self.template_name, context)

    def post(self, request):
        form = DetailForm(request.POST, request.FILES)
        if form.is_valid():
            form.save()
            return redirect('user')

        context = {'form': form}
        return render(request, self.template_name, context)

```

and my **`user.html (template)`** is:

```auto
<form method="post">
    {% csrf_token %}
    {{ form.as_p }}
    <button type="submit">Submit</button>
</form>

```

Every time I go to `localhost:8000/user` and fill the form and click on Submit Button, it gives me following error on frontend:  
`No File Chosen`  
and also the following statement appears above the File Selection Button:  
`This field is required`.

I shall appreciate for the help. Thanks

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [April 4, 2022, 1:43pm UTC](https://forum.djangoproject.com/t/how-do-i-upload-a-file-in-django-onetoone-models-filefield-using-form/12996/2 "2022-04-04T13:43:59Z")

</div>

> [@khaw235](#):
>
> ` form = DetailForm(request.POST, request.FILES)`

You’re not supplying the instance parameter when you’re building the form on the POST. There’s no way for this form to be associated with a User. (The form does not have the `user` field in it, so that field needs to come from “somewhere” for the model to be complete.)

Also see the docs at [File Uploads | Django documentation | Django](https://docs.djangoproject.com/en/4.0/topics/http/file-uploads/#basic-file-uploads) for the `enctype` setting needing to be made in the form template.

---

<div class="post-metadata">

**Author:** ![khaw235](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/khaw235/32/6372_2.png) [@khaw235](https://forum.djangoproject.com/u/khaw235)\
**Post date:** [April 4, 2022, 2:14pm UTC](https://forum.djangoproject.com/t/how-do-i-upload-a-file-in-django-onetoone-models-filefield-using-form/12996/3 "2022-04-04T14:14:30Z")

</div>

@KenWhitesell I did what you said, now my `user.html looks like below:

```auto
<form method="post" enctype="multipart/form-data"></form>>
    {% csrf_token %}
    {{ form.as_p }}
    <button type="submit">Submit</button>
</form>

```

and my `views.py` looks like below:

```auto
clas UserAPI(View):
    template_name = 'accounts/user.html'

    def get(self, request):
        form = DetailForm(instance=request.user)

        context = {'form': form}
        return render(request, self.template_name, context)

    def post(self, request):
        form = DetailForm(request.POST, request.FILES, instance=request.user)
        if form.is_valid():
            form.save()
            return redirect('user')

        context = {'form': form}
        return render(request, self.template_name, context)

```

Now, it does not show errors, but nothing else happens. Actually earlier I was debugging the code and it was working fine till this line `form = DetailForm(request.POST, request.FILES, instance=request.user)`, but now it is not even executing the same line.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [April 4, 2022, 2:44pm UTC](https://forum.djangoproject.com/t/how-do-i-upload-a-file-in-django-onetoone-models-filefield-using-form/12996/4 "2022-04-04T14:44:18Z")

</div>

> [@khaw235](#):
>
> `<form method="post" enctype="multipart/form-data"></form>>`

You’re closing your form (`</form>`) before the form itself.

---

<div class="post-metadata">

**Author:** ![khaw235](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/khaw235/32/6372_2.png) [@khaw235](https://forum.djangoproject.com/u/khaw235)\
**Post date:** [April 4, 2022, 2:53pm UTC](https://forum.djangoproject.com/t/how-do-i-upload-a-file-in-django-onetoone-models-filefield-using-form/12996/5 "2022-04-04T14:53:10Z")

</div>

OK that was some serious hidden mistake. Thanks @KenWhitesell for pointing it out. By the way, I did some changings after reading the documentation. I added `/` at the end of `upload_to='csv_files' in `Detail`model. and from some internet search, it was recommended to change`instance=request.user`to`initial={‘user’: request.user}`, and also added `else: print(form.errors`after the`if form.is\_valid` statement to view the errors.

It supposed to be working, but now I am getting the following error:  
`django.db.utils.IntegrityError: NOT NULL constraint failed: accounts_detail.user_id`

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [April 4, 2022, 2:55pm UTC](https://forum.djangoproject.com/t/how-do-i-upload-a-file-in-django-onetoone-models-filefield-using-form/12996/6 "2022-04-04T14:55:53Z")

</div>

> [@khaw235](#):
>
> `and from some internet search, it was recommended to change` instance=request.user `to` initial={‘user’: request.user} `,`

I disagree. You typically use `initial` for a “regular” (non-model) form, but `instance` when you’re working with a Model Form. Also, `user` is not a field in the form, so setting an initial value for it is meaningless.

---

<div class="post-metadata">

**Author:** ![khaw235](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/khaw235/32/6372_2.png) [@khaw235](https://forum.djangoproject.com/u/khaw235)\
**Post date:** [April 4, 2022, 3:00pm UTC](https://forum.djangoproject.com/t/how-do-i-upload-a-file-in-django-onetoone-models-filefield-using-form/12996/7 "2022-04-04T15:00:26Z")

</div>

OK @KenWhitesell , I changed it back to `instance.request.user`, but now it is showing the same error `No file chosen`, however the error `This field is required` is gone. And I added a print statement `print("Success")` after the `if form.is_valid()` and it got printed in the console, that means form is validated successfully, but `form.save()` is not being worked correctly.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [April 4, 2022, 3:11pm UTC](https://forum.djangoproject.com/t/how-do-i-upload-a-file-in-django-onetoone-models-filefield-using-form/12996/8 "2022-04-04T15:11:41Z")

</div>

Can you post your current form and view with your most recent changes / corrections?

---

<div class="post-metadata">

**Author:** ![khaw235](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/khaw235/32/6372_2.png) [@khaw235](https://forum.djangoproject.com/u/khaw235)\
**Post date:** [April 4, 2022, 4:02pm UTC](https://forum.djangoproject.com/t/how-do-i-upload-a-file-in-django-onetoone-models-filefield-using-form/12996/9 "2022-04-04T16:02:57Z")

</div>

Yes @KenWhitesell , below is my current **`models.py`** :

```auto
class Detail(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    file = models.FileField(verbose_name="CSV File", upload_to='csv_files/')
    file_desc = models.TextField("CSV File Description")

    def __str__ (self):
        return ("{} ({} {})".format(self.user.email, self.user.first_name, self.user.last_name))

```

below is my current **`forms.py`** :

```auto
class DetailForm(ModelForm):
    class Meta:
        model = Detail
        fields = ['file', 'file_desc']

```

below is my current **`views.py`** :

```auto
class UserAPI(View)
    template_name = 'accounts/user.html'

    def get(self, request):
        form = DetailForm(instance=request.user)

        context = {'form': form}
        return render(request, self.template_name, context)

    def post(self, request):
        form = DetailForm(request.POST, request.FILES, instance=request.user)

        if form.is_valid():
            form.save()
            return redirect('user')
        else:
            print(form.errors)

        context = {'form': form}
        return render(request, self.template_name, context)

```

and below is my current **`user.html`** ::

```auto
<form method="post" enctype="multipart/form-data">
    {% csrf_token %}
    {{ form.as_p }}
    <button type="submit">Submit</button>
</form>

```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [April 4, 2022, 5:37pm UTC](https://forum.djangoproject.com/t/how-do-i-upload-a-file-in-django-onetoone-models-filefield-using-form/12996/10 "2022-04-04T17:37:01Z")

</div>

First, the `instance` parameter should be the instance of `Detail` that is being modified by the data from this form, not of request.user.

Also, you should create the instance of detail before trying to submit this form which is trying to update the Detail instance.

So for example, your `get` method could start like this:  
`detail, created = Detail.objects.get_or_create(user=request.user)`  
`form = DetailForm(instance=detail)`

Your `post` method doesn’t need to do the `get_or_create`, it could just do a `get`, because the `get` method will create the Detail object if it needs to be created.

---

<div class="post-metadata">

**Author:** ![khaw235](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/khaw235/32/6372_2.png) [@khaw235](https://forum.djangoproject.com/u/khaw235)\
**Post date:** [April 4, 2022, 9:14pm UTC](https://forum.djangoproject.com/t/how-do-i-upload-a-file-in-django-onetoone-models-filefield-using-form/12996/11 "2022-04-04T21:14:46Z")

</div>

Hi @KenWhitesell I just updated my `get()` and `post()` method as you said, that first to create the `Detail` instance then pass it to the `instance` parameter . So my new code of **`views.py`** looks like below:

```auto
class UserAPI(View):
    template_name = 'accounts/user.html'

    def get(self, request):
        detail, created = Detail.objects.get_or_create(user=request.user)
        form = DetailForm(instance=detail)

        context = {'form': form}
        return render(request, self.template_name, context)

    def post(self, request):
        detail, created = Detail.objects.get(user=request.user)
        form = DetailForm(request.POST, request.FILES, instance=detail)
        if form.is_valid():
            form.save()
            return redirect('user')
        else:
            print(form.errors)

        context = {'form': form}
        return render(request, self.template_name, context)

```

But, now I am getting the below error:  
`accounts.models.Detail.DoesNotExist: Detail matching query does not exist.`

The full details of this `Traceback` error is below:

```auto
Traceback Switch to copy-and-paste view
C:\Users\Khubaib Khawar\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\handlers\exception.py, line 55, in inner
                response = get_response(request) …
Local vars
C:\Users\Khubaib Khawar\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\handlers\base.py, line 197, in _get_response
                response = wrapped_callback(request, *callback_args, **callback_kwargs) …
Local vars
C:\Users\Khubaib Khawar\AppData\Local\Programs\Python\Python310\lib\site-packages\django\views\generic\base.py, line 84, in view
            return self.dispatch(request, *args, **kwargs) …
Local vars
C:\Users\Khubaib Khawar\AppData\Local\Programs\Python\Python310\lib\site-packages\django\views\generic\base.py, line 119, in dispatch
        return handler(request, *args, **kwargs) …
Local vars
C:\Users\Khubaib Khawar\Downloads\Meistery\Round2\backend_dev_trial_ass_r2\accounts\views.py, line 270, in post
        detail, created = Detail.objects.get(user=request.user) …
Local vars
C:\Users\Khubaib Khawar\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\models\manager.py, line 85, in manager_method
                return getattr(self.get_queryset(), name)(*args, **kwargs) …
Local vars
C:\Users\Khubaib Khawar\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\models\query.py, line 496, in get
            raise self.model.DoesNotExist( …
Local vars

```

---

<div class="post-metadata">

**Author:** ![khaw235](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/khaw235/32/6372_2.png) [@khaw235](https://forum.djangoproject.com/u/khaw235)\
**Post date:** [April 4, 2022, 9:31pm UTC](https://forum.djangoproject.com/t/how-do-i-upload-a-file-in-django-onetoone-models-filefield-using-form/12996/12 "2022-04-04T21:31:20Z")

</div>

Hi @KenWhitesell I just figured out that I did not refresh the page after saving the `views.py()` and directly entered the data and submitted, which in result, only executed the `post()` method of my `UserAPI` view, and as `get()` was not executed, so `Detail` instance was not created and that’s why I was having the error I mentioned in my above reply.

Furthermore, I also figured out that only `get_or_create()` method returns a tuple, so I had to use `detail, created` only in the `get()` method, and just the `detail` in `post()` method.

So, finally it is working perfect. Thank you so much for the time you gave. You have always been a great help in my Django projects.

Is there a possibility that I get your email address or contact number? Thanks again.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [April 4, 2022, 9:32pm UTC](https://forum.djangoproject.com/t/how-do-i-upload-a-file-in-django-onetoone-models-filefield-using-form/12996/13 "2022-04-04T21:32:17Z")

</div>

> [@khaw235](#):
>
> `detail, created = Detail.objects.get(user=request.user)`

What’s the appropriate syntax for a `get` query?

---

<div class="post-metadata">

**Author:** ![khaw235](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/khaw235/32/6372_2.png) [@khaw235](https://forum.djangoproject.com/u/khaw235)\
**Post date:** [April 4, 2022, 9:33pm UTC](https://forum.djangoproject.com/t/how-do-i-upload-a-file-in-django-onetoone-models-filefield-using-form/12996/14 "2022-04-04T21:33:09Z")

</div>

Hey, please read my latest reply.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [April 4, 2022, 9:33pm UTC](https://forum.djangoproject.com/t/how-do-i-upload-a-file-in-django-onetoone-models-filefield-using-form/12996/15 "2022-04-04T21:33:39Z")

</div>

Yep, saw it - both messages were posted at about the same time.

---

<div class="post-metadata">

**Author:** ![khaw235](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/khaw235/32/6372_2.png) [@khaw235](https://forum.djangoproject.com/u/khaw235)\
**Post date:** [April 4, 2022, 9:34pm UTC](https://forum.djangoproject.com/t/how-do-i-upload-a-file-in-django-onetoone-models-filefield-using-form/12996/16 "2022-04-04T21:34:59Z")

</div>

Yes, thanks again. Did you read my request at the end of my reply?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [April 4, 2022, 9:35pm UTC](https://forum.djangoproject.com/t/how-do-i-upload-a-file-in-django-onetoone-models-filefield-using-form/12996/17 "2022-04-04T21:35:45Z")

</div>

Yes I did. I’m going to pass on that though, sorry.

---

<div class="post-metadata">

**Author:** ![khaw235](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/khaw235/32/6372_2.png) [@khaw235](https://forum.djangoproject.com/u/khaw235)\
**Post date:** [April 4, 2022, 9:36pm UTC](https://forum.djangoproject.com/t/how-do-i-upload-a-file-in-django-onetoone-models-filefield-using-form/12996/18 "2022-04-04T21:36:48Z")

</div>

No worries. Have a good day!
