Python 额外的;“行级”;ListView django中的数据
我正在编写我的第一个django应用程序,似乎无法通过ListView将“行级”数据传递给模板。具体地说,我试图使用PollListView显示所有民意调查及其相应的投票信息 目前我只能通过模板的所有投票,但只想通过属于特定投票的投票 型号.pyPython 额外的;“行级”;ListView django中的数据,python,django,listview,django-class-based-views,Python,Django,Listview,Django Class Based Views,我正在编写我的第一个django应用程序,似乎无法通过ListView将“行级”数据传递给模板。具体地说,我试图使用PollListView显示所有民意调查及其相应的投票信息 目前我只能通过模板的所有投票,但只想通过属于特定投票的投票 型号.py class Poll(models.Model): user = models.ForeignKey(User, unique=False, blank=False, db_index=True) title = models.Char
class Poll(models.Model):
user = models.ForeignKey(User, unique=False, blank=False, db_index=True)
title = models.CharField(max_length=80)
class Vote(models.Model):
poll = models.ForeignKey(Poll, unique=False, blank=False, db_index=True)
user = models.ForeignKey(User, unique=False, blank=True, null=True, db_index=True)
vote = models.CharField(max_length=30, blank=False, default='unset', choices=choices)
class PollListView(ListView):
model = Poll
template_name = 'homepage.html'
context_object_name="poll_list"
def get_context_data(self, **kwargs):
context = super(PollListView, self).get_context_data(**kwargs)
context['vote_list'] = Vote.objects.all()
return context
urlpatterns = patterns('',
...
url(r'^$', PollListView.as_view(), name="poll-list"),
}
class PollListView(ListView):
queryset = Poll.objects.all().prefetch_related('votes')
视图.py
class Poll(models.Model):
user = models.ForeignKey(User, unique=False, blank=False, db_index=True)
title = models.CharField(max_length=80)
class Vote(models.Model):
poll = models.ForeignKey(Poll, unique=False, blank=False, db_index=True)
user = models.ForeignKey(User, unique=False, blank=True, null=True, db_index=True)
vote = models.CharField(max_length=30, blank=False, default='unset', choices=choices)
class PollListView(ListView):
model = Poll
template_name = 'homepage.html'
context_object_name="poll_list"
def get_context_data(self, **kwargs):
context = super(PollListView, self).get_context_data(**kwargs)
context['vote_list'] = Vote.objects.all()
return context
urlpatterns = patterns('',
...
url(r'^$', PollListView.as_view(), name="poll-list"),
}
class PollListView(ListView):
queryset = Poll.objects.all().prefetch_related('votes')
url.py
class Poll(models.Model):
user = models.ForeignKey(User, unique=False, blank=False, db_index=True)
title = models.CharField(max_length=80)
class Vote(models.Model):
poll = models.ForeignKey(Poll, unique=False, blank=False, db_index=True)
user = models.ForeignKey(User, unique=False, blank=True, null=True, db_index=True)
vote = models.CharField(max_length=30, blank=False, default='unset', choices=choices)
class PollListView(ListView):
model = Poll
template_name = 'homepage.html'
context_object_name="poll_list"
def get_context_data(self, **kwargs):
context = super(PollListView, self).get_context_data(**kwargs)
context['vote_list'] = Vote.objects.all()
return context
urlpatterns = patterns('',
...
url(r'^$', PollListView.as_view(), name="poll-list"),
}
class PollListView(ListView):
queryset = Poll.objects.all().prefetch_related('votes')
homepage.html
{% for poll in poll_list %}
{{ poll.title }}
{% for vote in vote_list %}
{{ vote.id }} {{ vote.vote }}
{% endfor %}
{% endfor %}
这似乎是一项简单的任务,但我似乎不知道如何使用基于类的视图来实现这一点。我应该使用mixin还是额外的上下文?覆盖查询集?或者我应该使用基于函数的视图来解决这个问题
任何帮助都将不胜感激。我不确定它是否有效,但您可以尝试以下方法: models.py(投票类) 视图.py
class Poll(models.Model):
user = models.ForeignKey(User, unique=False, blank=False, db_index=True)
title = models.CharField(max_length=80)
class Vote(models.Model):
poll = models.ForeignKey(Poll, unique=False, blank=False, db_index=True)
user = models.ForeignKey(User, unique=False, blank=True, null=True, db_index=True)
vote = models.CharField(max_length=30, blank=False, default='unset', choices=choices)
class PollListView(ListView):
model = Poll
template_name = 'homepage.html'
context_object_name="poll_list"
def get_context_data(self, **kwargs):
context = super(PollListView, self).get_context_data(**kwargs)
context['vote_list'] = Vote.objects.all()
return context
urlpatterns = patterns('',
...
url(r'^$', PollListView.as_view(), name="poll-list"),
}
class PollListView(ListView):
queryset = Poll.objects.all().prefetch_related('votes')
使用该选项,您可以访问相关投票:
模板
{% for poll in poll_list %}
{{ poll.title }}
{% for vote in poll.votes.all %}
{{ vote.id }} {{ vote.vote }}
{% endfor %}
{% endfor %}
你要的是所有的选票。我想您应该使用
Vote.objects.filter(poll=instance)
。@RobL我没有得到错误,而是得到了每个投票返回的所有投票。因此,如果每次投票都有1票,而我有3票,那么我得到的是PollA-3票,PollB-3票,PollC-3票。@RobL感谢您的反馈。我尝试了你的建议(Vote.objects.filter(poll=instance)使用self.instance但出现以下错误:“PollListView”对象没有属性“instance”是的,很抱歉。@mariodev在下面有它。谢谢你的回答。我将查询集添加到ListView中。没有错误,但变量在模板中不可用。我是否需要添加多个字段来轮询才能工作?@Darth否,它使用s反向关系。是否从视图中删除了model
属性?我删除了model属性,但运气不佳。是否需要定义“投票集”某处?我应该覆盖get_queryset方法还是在类定义中设置属性就足够了?@Darth我已经更新了我的帖子,请在更改后再试一次。如果不起作用,我会自己尝试。。