python - How to use 2 ForeignKey filters one by one in 1 view - Django? -
hello , thank your answer:
my taks: show article, show 3 questions (related article), show 3 answer each queston (related these questions). coomon test page.
my models:
class step(models.model): #main article 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): #questios related article. 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): #answers related questions 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 should works
i use identificator in url, show django article should show. helps me filtrs questions article. , should filted answers each question, have got previously. there many articles, questions , answers, can't use objects().all.
url(r'^question(?p<question_id>[0-9]+)$', 'bakot.views.question', name='question'), #urls.py def question(request, question_id): stepfields = get_object_or_404(step, id = question_id) #get article questionship = question.objects.filter(step_id = question_id) #get questions, related article. answership = questionship.prefetch_related().all #this doesn't work. , couldn't use coommon filter, don't know id them. or set id , show same answers 3 questions. context = { "stepfieldst" : stepfields, "questionship" : questionship, "answership" : answership, } return render(request, 'bakot/question.html', context)
how show in template: (the part questions ans answers)
{% block question_area %} {% 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"> {% 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 %}
there few things here provide best solution.
first, don't need include questionship
, answership
in context. template can them via stepfields.questions
, question.answers
.
next, limit list 3, implement template filter. done using slices, this:
register = template.library() @register.filter def limit_to_3(list, arg): return list[0:arg]
you then, in template, change {% %}
loops format:
{% question in stepfieldst.questions|limit:3 %} {% answer in question.answers|limit:3 %} {% endfor %} {% endfor %}
depending if number of questions , answers per article low enough querying them less burdensome multiple queries, this:
try: stepfieldst = step.objects.select_related(questions__answers).filter(id = question_id) except step.doesnotexist: raise http404
this select related questions , answers in single query.
Comments
Post a Comment