Answer the question
In order to leave comments, you need to log in
How to cascade call ForeignKey elements in one view in Django?
Task: The general model of the article - 3 questions cling to it through ForeignKey - 3 answer options cling to each question through ForeignKey. (there are a lot of questions and answers, so you can’t output through objects (). all).
class Step(models.Model): #Основная статья
title = models.CharField(max_length=200)
description = models.CharField(max_length=200)
annotation = models.TextField()
main_text = models.TextField()
def __str__(self):
return self.title
class Question(models.Model): #Модель вопросов
step = models.ForeignKey(Step, on_delete=models.CASCADE)
title = models.CharField(max_length=200, default = "pages")
question_text = models.TextField()
question_name = models.CharField(max_length=40, help_text="английские буквы", blank=True, null=True)
class Answer(models.Model): #Модель ответов
question = models.ForeignKey(Question, on_delete=models.CASCADE)
choice_text = models.TextField()
votes = models.IntegerField(default=0)
answer_name = models.CharField(max_length=40, help_text="английские буквы", blank=True, null=True)
url(r'^question(?P<question_id>[0-9]+)$', 'bakot.views.question', name='question'),
def question(request, question_id):
questionship = Question.objects.filter(step_id = question_id) #step_id - это номер, которым вытаскиваем относящиеся вопросы.
answership = questionship.prefetch_related().all # а вот эта штука и не работает собственно.
context = {
"questionship" : questionship,
"answership" : answership,
}
return render(request, 'bakot/question.html', context)
{% block question_area %}
{% for question in questionship %}
<div class="small-12 medium-12 large-12 test-remark">
<legend><strong>{{ question.title }} </strong>{{ question.question_text }}</legend>
<ul class="menu vertical">
{% for answer in answership %}
<li>
<ul class="menu test-answer-padding navigation_hover_link11">
<li class="test-dot-padding"><input type="radio" name="{{ question.question_name }}" value="{{ answer.answer_name }}" id="pokemonRed" required></li>
<li><label for="pokemonRed">{{ answer.choice_text }}</label>
</ul>
</li>
{% endfor %}
</ul>
</div>
{% endfor %}
{% endblock %}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question