Answer the question
In order to leave comments, you need to log in
How to make double cascade filter in Django view.py?
Guys, hello everyone.
Task: The general model of the article - 3 questions cling to it through ForeignKey - 3 answer options cling to each question through ForeignKey.
My implementation:
1) I pass question1_id through the url pattern
url(r'^question(?P<question1_id>[0-9]+)$', 'bakot.views.question', name='question')
def question(request, question1_id):
stepfields = get_object_or_404(Step, id = question1_id)
questionship = Question.objects.filter(step_id = question1_id)
answership = Answer.objects.filter(question_id = question1_id)
context = {
"stepfields" : stepfields,
"questionship" : questionship,
"answership" : answership,
}
return render(request, 'bakot/question.html', context)
def question(request, question1_id):
stepfields = get_object_or_404(Step, id = question1_id)
questionship = Question.objects.filter(step_id = question1_id)
answership = Answer.objects.filter(question_id = questionship.id) # 1 попытка
answership = Answer.objects.filter(question_id = questionship(request(id))) #2 попытка
context = {
"stepfields" : stepfields,
"questionship" : questionship,
"answership" : answership,
}
return render(request, 'bakot/question.html', context)
Answer the question
In order to leave comments, you need to log in
I don’t understand why step, when all the questions are displayed in the picture for your article at once?
Pull out questions and related answers like this Question.objects.filter(article=).prefetch_related("answerset"). You didn’t specify the model declaration, so I randomly wrote, if there is no answerset, in the dev environment, djanga will swear by itself, 1.8 will definitely swear
Answers pull querstion.answer.all()
https://docs.djangoproject.com/es/1.9/ref/ models/q...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question