# Set ForeignKey in an UpdateView

**URL:** <https://forum.djangoproject.com/t/set-foreignkey-in-an-updateview/36788>\
**Category:** Forms & APIs\
**Created:** [November 27, 2024, 12:38pm UTC](https://forum.djangoproject.com/t/set-foreignkey-in-an-updateview/36788 "2024-11-27T12:38:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Suhel-El-Salim](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/suhel-el-salim/32/22862_2.png) [@Suhel-El-Salim](https://forum.djangoproject.com/u/Suhel-El-Salim)\
**Post date:** [November 27, 2024, 12:38pm UTC](https://forum.djangoproject.com/t/set-foreignkey-in-an-updateview/36788/1 "2024-11-27T12:38:18Z")

</div>

Hello @all,

I have two models, Formular and Person. The model Formular has a ForeignKey person (each Formular belongs to one Person).

When I write an UpdateView, the view gets the data including the ForeignKey out of the database. I don`t want to display the ForeignKey in the form nor take it from the url. When I update (send) the form, it doesn’t work because missing ForeignKey.

I want to manage to write the ForeignKey in the backend. I tried it with def form\_valid(self, form): but I didn’t get it to work.

Does anybody has a suggestion for me?

Best regards  
Suhel

---

<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:** [November 27, 2024, 12:47pm UTC](https://forum.djangoproject.com/t/set-foreignkey-in-an-updateview/36788/2 "2024-11-27T12:47:52Z")

</div>

Please post your UpdateView and the form you are using, along with the model being updated.

The direct answer to your question _may_ be to remove the ForeignKey field from the form, but in the absence of seeing the code, we’re not going to be able to be sure about that.

---

<div class="post-metadata">

**Author:** ![Suhel-El-Salim](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/suhel-el-salim/32/22862_2.png) [@Suhel-El-Salim](https://forum.djangoproject.com/u/Suhel-El-Salim)\
**Post date:** [November 27, 2024, 1:05pm UTC](https://forum.djangoproject.com/t/set-foreignkey-in-an-updateview/36788/3 "2024-11-27T13:05:06Z")

</div>

Hello Ken,

that was the solution. I had to exclude the ForeignKey field, then the update works.

```auto
class FormularForm(forms.ModelForm):
    
  class Meta:
    model = Formular
    fields = ' __all__'
    exclude = ('person',)
    widgets = {'GeldErhalten': forms.RadioSelect(choices=GELDERHALTEN),
               'Geburtsdatum': forms.DateInput(format='%Y-%m-%d', attrs={'type': 'date'})}

```

Thank you very much 🙂
