[Answered ]-Form for multiple models

2👍

You can create two separate forms having the required fields, for each model. Then show both forms in the template inside one html element. Both forms will get rendered and then submitted individually. You can then process the forms separately in the view.

class TopicForm(ModelForm):

    class Meta:
        model = Topic
        fields = ("title", ..)


class PostForm(ModelForm):

    class Meta:
        model = Post
        fields = ("body", ..)

In view:

form1 = TopicForm()
form2 = PostForm()

In template:

<form ...>
{{ form1 }}
{{ form2 }}
</form>

You can easily use form.save() and all other functions, without doing it all yourself.

Leave a comment