1👍
The instance
must match the form’s model, Comment_to_wish_list
. It doesn’t make sense to pass instance=wish_list
to the form, because the model doesn’t match.
You can set the comment_to
field after saving the form with commit=False
, in the same way that you set the person
field:
wish_list = get_object_or_404(Wish_list, pk=wish_list_id)
if request.method == 'POST':
form = Comment_to_wish_listForm(request.POST)
if form.is_valid():
instance=form.save(commit=False)
instance.person = request.user
instance.comment_to = wish_list
instance.save()
Source:stackexchange.com