我需要查询最后一次民意调查(问题和选择)作为表格

时间:2011-09-07 19:57:54

标签: django

class Question(models.Model):
    """This class represents a question. It can have 2 or more options."""
    created_on = models.DateTimeField(auto_now_add = 1)
    title = models.CharField(max_length = 200)
    slug = models.SlugField(unique = True, max_length = 200)

class Choice(models.Model):
    """This represents an answer to the Question, and has a foreignkey to it"""
    question = models.ForeignKey(Question)
    text = models.TextField()
    total_votes = models.IntegerField(default = 0)

我想查询最后一个问题而且它的选择我尝试但不能得到最后的形式

1 个答案:

答案 0 :(得分:0)

获取最后一项你可以做的事情:

q = Question.objects.order_by('id').reverse()[0]

并获得选择:

choices = q.choice_set.all()

总结一下,这就是你的观点的样子:

def last_q(request): 
  return render_to_response('poll/last.html', { 'last_q': Question.objects.order_by('id').reverse()[0] } ) 

这是你的模板:

{% for c in last_q.choice_set.all %}
 <input type="radio" name="choice" value="{{ c.id }}" />{{ c.text }}
{% endfor %}

类似的东西!