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
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.
Hello Ken,
that was the solution. I had to exclude the ForeignKey field, then the update works.
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