I have a url /<subject_id>/comments/new/ which renders a Django ModelForm. I am using a view class derived from FormView to process the form. I wish to do the following:
subject_idshould not appear on the rendered form.subject_idshould be added to the form prior tois_valid()being called, or if this is not possible should be added to theCommentinstance.
forms/comment_form.py:
class CommentForm(ModelForm):
class Meta:
model = Comment
fields = ['text']
views.py:
class CommentCreate(FormView):
form_class = CommentForm
def form_valid(self, form):
# Do some stuff to the validated Comment instance
# Maybe save the comment, maybe not
return super().form_valid(form)
How do I do this? If I add subject_id as a field in CommentForm then it appears on the rendered form. If I don't then the form is instantiated with subject_id present from `self.kwargs['subject_id'] and complains of an "unexpected keyword argument".