S
S
Sergey Nizhny Novgorod2016-01-15 17:03:37
Django
Sergey Nizhny Novgorod, 2016-01-15 17:03:37

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.
download?id=Tv5QnVlxCMAPY1LnWiuEQKiIzXB0
My implementation:
1) I pass question1_id through the url pattern

url(r'^question(?P<question1_id>[0-9]+)$', 'bakot.views.question', name='question')

2) I am writing a view in which I want to make logic:
- the article is loaded,
- through the filter I select questions that relate to this article.
- through the filter I select the answers that relate to each specific 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)

Problem: The article is displayed, the questions are displayed, the answers are displayed ( but these answers belong to one question, i.e. they are repeated in three questions).
those. it turns out that the filter goes by question_id = 1
Answer database model for one question (only answers with question_id = 1 are displayed on the page)
download?id=PLEUvuOHxUD8o4MwsKBBJrLs14Nb
What I tried:
In the question database, there is a parameter: id.
download?id=41txoPzHinSmH116yVjZsLDKhHeV
I think so, first we filter out all the questions that belong to this article, and then we pass their id to the question output filter.
download?id=MvmyrvryjRAd4TybjAxDPsbfFLGI
Type:
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)

In the end, it doesn't work.
Of course, you can simply set the id there with handles, but I think this is not good.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2016-01-15
@pacahon

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 question

Ask a Question

731 491 924 answers to any question