'Best way to pass random answers in Django Quiz App

I'm trying to build quiz app based on Cornell Notes, where the keywords will be the question with 4 answers (one correct answer and three random from other keys)

My models:

class Note(models.Model):
    topic = models.ForeignKey(Topic, on_delete=models.CASCADE, null=True, blank=True)
    title = models.CharField(max_length=128)
    summary = models.CharField(max_length=500)
    created = models.DateTimeField(auto_now_add=True)
    modified = models.DateTimeField(auto_now=True)
    id = models.UUIDField(default=uuid.uuid4, unique=True, primary_key=True, editable=False)

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


class Record(models.Model):
    note = models.ForeignKey(Note, on_delete=models.CASCADE, null=True, blank=True)
    key = models.CharField(max_length=100, blank=False, null=False)
    value = models.CharField(max_length=300, blank=False, null=False)
    created = models.DateTimeField(auto_now_add=True)
    id = models.UUIDField(default=uuid.uuid4, unique=True, primary_key=True, editable=False)

    def __str__(self):
        return f'{self.key} | {self.value}'

e.g. Note is called "Children of Zeus" and has 4 records (4 key and value pairs):

key: value

  • Apollo: Twin of Artemis. Is the god of the sun, music, medicine, and poetry.
  • Ares: god of war
  • Hermes: the god of boundaries and the travelers who cross them and the son of Zeus and Maia.
  • Hephaestus: god of blacksmiths, craftsmen, artisans, sculptors, and fire.

  • The question is, how can I easily generate 4 (number of records) questions in HTML with one correct and three incorrect answers each question using views.py and djagno templates?


    Sources

    This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

    Source: Stack Overflow

    Solution Source