# match two models query in html

**URL:** <https://forum.djangoproject.com/t/match-two-models-query-in-html/6162>\
**Category:** Using Django\
**Created:** [January 16, 2021, 3:37pm UTC](https://forum.djangoproject.com/t/match-two-models-query-in-html/6162 "2021-01-16T15:37:18Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![jujuna](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/jujuna/32/3143_2.png) [@jujuna](https://forum.djangoproject.com/u/jujuna)\
**Post date:** [January 16, 2021, 3:37pm UTC](https://forum.djangoproject.com/t/match-two-models-query-in-html/6162/1 "2021-01-16T15:37:18Z")

</div>

I have a question answer quiz. I want each form to be responsible for each query when I publish the forms in html. For example I have it now ![](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/2X/7/7d74564d5d5d4fb80ae4613ae696c37c5df8d7ee.png).  
I have to choose the question manually. I want it to happen automatically, i.e. no matter how many questions I have so many forms for each question.  
I do not understand what I have to change, view or formset should I use, please if you can help me change the code.

my code -\>  
**models.py**

```auto
from django.db import models

# Create your models here.
class Question(models.Model):
    question=models.CharField(max_length=100)
    answer_question=models.CharField(max_length=100, default=None)

    def __str__ (self):
        return self.question

    
class Answer(models.Model):
    questin=models.ForeignKey(Question, on_delete=models.CASCADE)
    answer=models.CharField(max_length=100,blank=True)

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

```

**forms.py**

```auto
from django import forms
from django.contrib.auth.models import User
from django.core.exceptions import ValidationError
from django.forms import ModelForm

from .models import Question,Answer

class QuestionForm(forms.ModelForm):
    class Meta:
        model=Question
        fields=" __all__"

class AnswerForm(forms.ModelForm):
    class Meta:
        model=Answer
        fields=" __all__"

```

**views.py**

```auto
from django.shortcuts import render
from django.shortcuts import render, HttpResponse
from django.http import HttpResponseRedirect
from django.shortcuts import redirect
from .forms import QuestionForm,AnswerForm
from .models import Question
import random

def home(request):
    form=QuestionForm
    if request.method=='POST':
        form=QuestionForm(request.POST)
        if form.is_valid():
            form.save()

    return render(request, "question/base.html", {"form":form})

def ans(request):
    form=AnswerForm
    question=Question.objects.all()
    if request.method=="POST":
        form=AnswerForm(request.POST)
        if form.is_valid():
            form.save()
            
    return render(request, "question/ans.html", {"form":form, "question":question})

```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [January 16, 2021, 3:41pm UTC](https://forum.djangoproject.com/t/match-two-models-query-in-html/6162/2 "2021-01-16T15:41:26Z")

</div>

Making sure I’m clear on this.

You have some number of “Question” objects.

You want to create a page that has that same number of forms on it, where each form is for a different question.

You want the question to be displayed as a label for each form, where the only form field is for the answer to that question.

Is my understanding correct?

---

<div class="post-metadata">

**Author:** ![jujuna](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/jujuna/32/3143_2.png) [@jujuna](https://forum.djangoproject.com/u/jujuna)\
**Post date:** [January 16, 2021, 3:45pm UTC](https://forum.djangoproject.com/t/match-two-models-query-in-html/6162/3 "2021-01-16T15:45:46Z")

</div>

I even want each input form to be responsible for the question automatically and not have to manually select the question. Take facebook for example. The comment field for each post belongs to that post huh right? I want to be like that. With the following structure: Question -\> Input of this query -\> Submit of this input. did you understand? @KenWhitesell

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [January 16, 2021, 3:49pm UTC](https://forum.djangoproject.com/t/match-two-models-query-in-html/6162/4 "2021-01-16T15:49:03Z")

</div>

> [@KenWhitesell](#):
>
> You want the question to be displayed as a label for each form, where the only form field is for the answer to that question.

Which is what I wrote here - is this not correct?

---

<div class="post-metadata">

**Author:** ![jujuna](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/jujuna/32/3143_2.png) [@jujuna](https://forum.djangoproject.com/u/jujuna)\
**Post date:** [January 16, 2021, 3:51pm UTC](https://forum.djangoproject.com/t/match-two-models-query-in-html/6162/5 "2021-01-16T15:51:01Z")

</div>

> [@KenWhitesell](#):
>
> You want the question to be displayed as a label for each form, where the only form field is for the answer to that question.

yes yes this is correct @KenWhitesell

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [January 16, 2021, 4:03pm UTC](https://forum.djangoproject.com/t/match-two-models-query-in-html/6162/6 "2021-01-16T16:03:42Z")

</div>

So yes, you are looking to use a [ModelFormset](https://docs.djangoproject.com/en/3.1/topics/forms/modelforms/#model-formsets), where each instance of the formset is a [custom model form](https://docs.djangoproject.com/en/3.1/topics/forms/modelforms/#changing-the-form). Each instance of that model form should have a hidden field to track the pk of the question along with the text of the question being used as the label for that form.

Also, you’ll probably want to make sure that your formset doesn’t generate any extra forms, and does not allow for new forms to be added or for forms to be removed.

What you want to keep in mind is that the class for your model formset should be Answer, not Question, and that you access the necessary fields from Question through your fk field.

So I would first suggest you try building a model formset for Answer with just the right number of answer forms, to get you comfortable with working with formsets. Then you can add the customization for the specific label on each one.

---

<div class="post-metadata">

**Author:** ![jujuna](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/jujuna/32/3143_2.png) [@jujuna](https://forum.djangoproject.com/u/jujuna)\
**Post date:** [January 16, 2021, 4:06pm UTC](https://forum.djangoproject.com/t/match-two-models-query-in-html/6162/7 "2021-01-16T16:06:15Z")

</div>

Thanks a lot I will review these links but if you have any other links that will help me more? In addition to documentation. Because documentation is always a bit difficult in my opinion

---

<div class="post-metadata">

**Author:** ![jujuna](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/jujuna/32/3143_2.png) [@jujuna](https://forum.djangoproject.com/u/jujuna)\
**Post date:** [January 17, 2021, 10:39am UTC](https://forum.djangoproject.com/t/match-two-models-query-in-html/6162/8 "2021-01-17T10:39:53Z")

</div>

are you here? I looked at the model formset and I think it will not work. I do not want to work with 2 forms at once. I want the Answer input to match the Question. with formset I think it works with 2 model forms. @KenWhitesell

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [January 17, 2021, 1:32pm UTC](https://forum.djangoproject.com/t/match-two-models-query-in-html/6162/9 "2021-01-17T13:32:02Z")

</div>

Yep, still here.

No, there are not two forms involved with this. It’s only one **form** , and that’s for your answer. The questions will be the labels. Each Answer form generated will have a different label, from the corresponding question.

---

<div class="post-metadata">

**Author:** ![jujuna](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/jujuna/32/3143_2.png) [@jujuna](https://forum.djangoproject.com/u/jujuna)\
**Post date:** [January 17, 2021, 1:37pm UTC](https://forum.djangoproject.com/t/match-two-models-query-in-html/6162/10 "2021-01-17T13:37:44Z")

</div>

Does the formset need instance? (Instance of questions) I could not understand how to point dynamically @KenWhitesell

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [January 17, 2021, 3:14pm UTC](https://forum.djangoproject.com/t/match-two-models-query-in-html/6162/11 "2021-01-17T15:14:31Z")

</div>

There’s actually another good post on here that you may find helpful. See: [Pass different parameters to each form in formset - #2 by datalowe](https://forum.djangoproject.com/t/pass-different-parameters-to-each-form-in-formset/4040/2)
