S
S
Sergey Nizhny Novgorod2016-01-16 03:00:50
Django
Sergey Nizhny Novgorod, 2016-01-16 03:00:50

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)

How it works:
I pass the question_id through the url with which I determine the number of the article from which the work begins. But then I can no longer get answers that relate to the question that we received before.
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)

Well, I'm outputting this case to a template:
{% block question_area %}
  {% for question in questionship %}
  <div class="small-12 medium-12 large-12 test-remark">
    <legend><strong>{{ question.title }}&nbsp;</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 %}

I will be grateful for help

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey K, 2016-01-16
@Terras

https://docs.djangoproject.com/es/1.9/ref/models/r...

{% for answer in question.answer_set.all  %}
...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question