N
N
Neoliz2016-09-15 10:57:28
Django
Neoliz, 2016-09-15 10:57:28

How to make a formset from two related models in django?

The crux of the matter is this. There are two models, question and answers.
I need to make a formset and define it's own validation. There is no problem with validation. Problem creating set. I read the doc, I've already googled a lot.
The option to create forms.FORM or forms.ModelForm doesn't matter(BaseFormSet ot BaseModelFormSet).
I did adding questions with answers in the admin panel. Now I need to display a test for the user from random several questions, for each there is only one correct answer (RadioSelect() use). And they will check whether they answered everything and whether they answered correctly. As I understand it, you need to create a form for one question with answers, and make a set of forms from this form. But there also the initial data must be crammed in (i.e. question and answers).
The problem is only with the creation of the form. I can't figure out how to do it. Tell me where to look.
here are the models:

class Question(models.Model):
    question = models.TextField("Вопрос:")
    date_created = models.DateTimeField("Дата создания вопроса",
                                        auto_now_add=True)
    active = models.BooleanField("Активен ли вопрос", default=True)

    def __str__(self):
        return self.question

    class Meta:
        verbose_name = "Вопрос"
        verbose_name_plural = "Вопросы"


class Answers(models.Model):
    question = models.ForeignKey(Question, verbose_name="Вопрос: ")
    answer = models.CharField("Ответ:", max_length=250)
    correctly = models.BooleanField("Правильный ответ", default=False)

    def __str__(self):
        return self.answer

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question