python - Filter queryset in django templates -
i have model followed:
class venture(models.model): name = models.charfield(_('name'), max_length=255) created = models.datetimefield(editable=false) modified = models.datetimefield() class questionset(models.model): title = models.charfield(_(u'title'), max_length=100) class question(models.model): title = models.charfield(_(u'title'), max_length=255) qset = models.foreignkey(questionset, related_name='questions') class answer(models.model): question = models.foreignkey(question, related_name='answers') responder = models.foreignkey(user) venture = models.foreignkey(venture, related_name='answers') text = models.textfield(_(u'answer'), blank=true, null=true) timestamp = models.datetimefield(auto_now_add=true) there exists set of predefined questions users. each venture, have page each questionset lists questions in set , loop on questions followed:
<div> {{ venture.name }} </div> {% question in qset.questions.all %} <div class="qset-question control-group"> {{ question.title }} {# how access answer current venture? #} </div> {% endfor %} the question best way answer of question current venture. want output information answer here.
any appreciated.
i solved problem creating custom template tage. here code:
@register.assignment_tag def question_answer(venture, question): answers = question.answers.filter(venture=venture) return answers[0] if answers else none then used this:
{% question_answer venture question answer %} {{ answer }}
Comments
Post a Comment