Answer the question
In order to leave comments, you need to log in
How to correctly display all contests with the data of these contests in the template?
models.py
#модель конкурса
class Quest(models.Model):
title = models.CharField(max_length = 500)
def __str__(self):
return self.title
#модель вопросов конкурса
class Questions(models.Model):
title = models.CharField('Название вопроса', max_length=500)
question_above = models.CharField('Ответ', max_length=1000)
quest = models.ForeignKey(Quest, on_delete=models.CASCADE, related_name='quest')
def __str__(self):
return self.title
#варианты ответов конкурса
class Questions_var(models.Model):
question = models.CharField('Вариант ответа', max_length=500)
questions = models.ForeignKey(Questions, on_delete=models.CASCADE, related_name='questions')
Answer the question
In order to leave comments, you need to log in
I would add a function for getting all the questions in the Quest (for example get_questions), and in the Questions model getting all the answers as a separate function (for example get_answers). And then in the template it would be possible to output:
{% for quest in quests %}
{% for question in quest.get_questions %}
{% for answer in question .get_answers %}
..... HTML .....
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question