【发布时间】:2015-11-13 00:36:39
【问题描述】:
我一直在关注 Django 入门教程 (https://docs.djangoproject.com/en/1.8/intro/tutorial05/)
我决定从现在开始进行一些修改以测试我的技能。
具体来说,我打算为 ResultsView 通用视图实现自定义 get_queryset。
类似这样的:
# views.py
class ResultsView(generic.DetailView):
model = Question
template_name = 'polls/results.html'
def get_queryset(self):
'''
Make sure we are displaying results for choices which have 1+ votes
'''
return Question.objects.get ...
基本上,我的目标是仅针对至少 1 票的选择返回问题的选择。
所以我在 Django 的 shell 中尝试了这样的东西:
# Django shell
q = Question.objects.get(pk=1)
q.choice_set.filter(votes=1)
[<Choice: Not much>]
这里我得到 pk = 1 的问题,然后根据choice_set(Choice 模型的 fk 指的是 Question 模型)进行过滤。
我试图弄清楚如何在我的 views.py 中实现这一点,以便它仅返回问题的内容(即选择),仅针对具有 1+ 票的选择(即显示所有选择有相关票数,但选择票数为 0)。
为了完整起见,这里是实际模板(polls/results.html):
<h1>{{ question.question_text }}</h1>
<ul>
{% for choice in question.choice_set.all %}
<li>{{ choice.choice_text }} -- {{ choice.votes }} vote{{ choice.votes|pluralize }}</li> {# pluralize used to automatically add "s" for values with 0 or 2+ choice.votes #}
{% endfor %}
</ul>
<a href="{% url 'polls:detail' question.id %}">Vote again?</a>
模型
# models.py
Class Question(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
def __str__(self):
return self.question_text
def was_published_recently(self):
now = timezone.now()
return now - datetime.timedelta(days=1) <= self.pub_date <= now
class Choice(models.Model):
question = models.ForeignKey(Question)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
def __str__(self):
return self.choice_text
【问题讨论】:
标签: python django django-models django-queryset