Insert Alternatives into a Model in Django

Asked

Viewed 130 times

0

Good evening. I’m a beginner in Django, I’m trying to make a simple platform, kind of a quiz and I’m having a question. I have the following class:

class Questao(models.Model):
    subdominio = models.ManyToManyField(SubDominio)
    situacao = models.OneToOneField(Situacao, on_delete=models.CASCADE)
    questao = models.TextField()

    def __str__(self):
        return self.questao

    class Meta:
        verbose_name = "Questão"
        verbose_name_plural = "Questões"
        ordering = ['subdominio__name']

Where Question is Related to Situation:

class Situacao(models.Model):
    enunciado = models.TextField()
    certificado = models.ManyToManyField(Certificado)

    def __str__(self):
        return '{}...'.format(self.enunciado[:80])

    class Meta:
        verbose_name = "Situação"
        verbose_name_plural = "Situações"

And the certified variable that is an n-to-n relation is basically like a certain Quiz category. Anyway, I would like to add in the Question Model several alternatives: letter a, b, c, etc (which I would already add for example a boolean, indicating which one would be the correct one). How could I do this and add the alternatives by the Question category directly in Django Admin?

What I had done before was basically this here:

class Alternativa(models.Model):
    questao = models.ForeignKey(Questao, on_delete=models.CASCADE)
    resposta = models.TextField()
    correta = models.BooleanField(default=False)

    def __str__(self):
        return self.resposta

    class Meta:
        verbose_name = "Alternativa"
        verbose_name_plural = "Alternativas"

But I always had to add a certain alternative and relate to the issue that I had created, and I wanted something more straightforward by adding the same question.

Thanks for any help and in case I haven’t been clear I try to edit the question.

--- Edited

I’d like it to look something like this:

inserir a descrição da imagem aqui

It would have something like adding this question, and I could put A, B, C, D or 1, 2, 3, 4 or whatever, and then when I selected that I would add the options of the alternatives, which would be marked as correct and which would not. Get it? So I wouldn’t have to add the alternatives one by one and make a connection with the question on the panel.

  • The idea of your alternative model is correct. It seems that the problem is to set the screen in admin. You even customize the admin form?

  • I didn’t get around to it. How do I do it? On my admin screen I just made the class statement in admin.py and registered it, no big deal.

  • Da para vc simular a imagem da tela que vc prentende e postar na pergunta? pode ser um desenho mesmo.

  • I edited the question and added at the end, Sidon. If you can help me I thank you. Thank you! And excuse the delay to answer.

1 answer

0

Hello, To do what you would like to use, you should models.py the modelchoicefield , and use the mode of choice with alternatives in the forms.py and thus use if if the letter is correct and a else to the other wrong.

Example forms.py

class quizform(ModelForm)
     model = quiz
      if escolha == "C":
            print ('Acertou Mizeravi')
      else: 
            print ('Errrrrrrou')

link how to use on models.py: https://docs.djangoproject.com/pt-br/2.1/ref/forms/fields/#modelchoicefield

I hope I’ve helped

And may the force be with you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.