<meta name="clckd" content="6301244fb7ed470eb1cd6b8c31d01ac4" />

Django Formset: Submit Two Django Forms At Once

Hello Devs, In this Post I'm gonna show you how can we submit two django forms at once and this is also called django inline forms, for example, I have two forms one is for Question and another one is for Answers, and I want an inline form, like one question and that question have four answers, ok so let's see how can we do that.

Source Code:- 

First of all create two tables in models.py:

class Question(models.Model):
    text = models.CharField(max_length=200, unique=True)
    created_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return str(self.text)

    #Reverse relationship
    def get_answers(self):
        return self.answers.all()   

class Answer(models.Model):
    text = models.CharField(max_length=200, verbose_name='Answer')
    correct = models.BooleanField()
    question = models.ForeignKey(Question, on_delete=models.CASCADE, related_name='answers')
    created_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return f"question {self.question.text}, answer: {self.text}. correct: {self.correct}"


Now create model forms in forms.py:

from django.forms.models import modelformset_factory

class QuestionsForm(forms.ModelForm):
    text = forms.CharField(label='Question', required=True)

    class Meta:
        model = Question
        fields = ['text']

AnswerFormset = modelformset_factory(
    fields=('text', 'correct'),
    widgets={ 'text': forms.TextInput(attrs={ 'class': 'form-control','placeholder': 'Enter Answer here'}

import modelformset_factory then use modelformset_factory this in that form in which you want to show extra fields. for example one question have multiple answers, so we modelformset_factory in answer form

Now create views for that forms.

def add_questions(request):
    ques= QuestionsForm()
    formset = AnswerFormset(queryset=Answer.objects.none())
    if request.method=='POST':
        ques= QuestionsForm(request.POST)
        formset = AnswerFormset(request.POST)
        if ques.is_valid() and formset.is_valid():
            ques = ques.save()
            for form in formset:
                answer = form.save(commit=False)
                answer.question = ques
            return redirect('home')
    return render(request, "add-ques.html", {'ques':ques, 'formset':formset})

def home(request):
    ques = Question.objects.all()
    context = {'ques':ques}
    return render(request, 'index.html', context)

Now render that form in your template


<form action="." method='post'>
        {% csrf_token %}
        {% for field in ques %}
        <div class="form-group">
            <label for="text">{{field.label}}:</label>
        {% endfor %}
        {% comment %} {{ques}}
        {{formset}} {% endcomment %}
        {{ formset.management_form }}
        {% for field in formset %}
        <div class="form-group mt-5 mb-5">
          <label for="text">{{field.label}}</label>
        {% endfor %}
        <button type="submit" class="btn btn-primary">Submit</button>


that's it and this is how your form look like

Image description


Image description


Source Code:- 

Thank you
Shivam Rohilla Python Developer


  • No Reviews Yet

Leave a Reply
Please Sign In For Comment.
Login or Signup
Shivam Rohilla
Hi, I'm Shivam Rohilla

Hi! My name is Shivam Rohilla. I am a Full Stack Web Developer, and I'm very passionate and dedicated to my work. With 2 years experience as a professional Full Stack Web Developer.

Follow Me:-

My social media links