Python 使用models.manager计算投票数
我正在使用models.manager计算投票数,但我不明白为什么投票数没有显示出来。投票系统工作(与管理员核实),但经理不工作 models.pyPython 使用models.manager计算投票数,python,django,Python,Django,我正在使用models.manager计算投票数,但我不明白为什么投票数没有显示出来。投票系统工作(与管理员核实),但经理不工作 models.py class PostVoteCountManager(models.Manager): def get_query_set(self): return super(PostVoteCountManager, self).get_query_set.annotate( votes=Count('vote
class PostVoteCountManager(models.Manager):
def get_query_set(self):
return super(PostVoteCountManager, self).get_query_set.annotate(
votes=Count('vote')).order_by("-votes")
class Post(models.Model):
rank_score = models.FloatField(default=0.0)
with_votes = PostVoteCountManager()
class Vote(models.Model):
voter = models.ForeignKey(User, on_delete=models.CASCADE)
post = models.ForeignKey(Post, on_delete=models.CASCADE)
def __str__(self):
return "%s voted %s" %(self.voter.username, self.post.title)
我的观点
class PostListView(ListView):
model = Post
template_name = 'community/home.html' # <app>/<model>_<viewtype>.html
context_object_name = 'posts'
#ordering = ['-date_posted']
queryset = Post.with_votes.all()
def get_context_data(self, **kwargs):
context = super(PostListView, self).get_context_data(**kwargs)
if self.request.user.is_authenticated:
voted = Vote.objects.filter(voter=self.request.user)
posts_in_page = [post.id for post in context["object_list"]]
voted = voted.filter(post_id__in=posts_in_page)
voted = voted.values_list('post_id', flat=True)
context["voted"] = voted
return context
类PostListView(ListView):
型号=员额
模板_name='community/home.html'#/_.html
上下文\对象\名称='posts'
#订购=['-发布日期']
queryset=Post.with_vows.all()
def获取上下文数据(自身,**kwargs):
context=super(PostListView,self)。获取上下文数据(**kwargs)
如果self.request.user.u经过身份验证:
投票=Vote.objects.filter(投票人=self.request.user)
posts_in_page=[post.id for post in context[“object_list”]]
已投票=已投票。筛选器(post\u id\u in=post\u in\u页面)
已投票=已投票。值列表('post\u id',flat=True)
上下文[“已投票”]=已投票
返回上下文
在html中我是这样做的
{% for post in posts %}
<form method="post" action="{% url 'vote' %}" class="vote_form">
<li> [{{ post.votes }}]
{{post}}
{% csrf_token %}
<input type="hidden" id="id_post" name="post" class="hidden_id" value="{{ post.pk }}" />
<input type="hidden" id="id_voter" name="voter" class="hidden_id" value="{{ user.pk }}" />
{% if not user.is_authenticated %}
<button disabled title="Please login to vote">+</button>
{% elif post.pk not in voted %}
<button>+</button>
{% else %}
<button>-</button>
{% endif %}
</form>
{% endform%}
{%for posts in posts%}
[{{post.voces}}]
{{post}}
{%csrf_令牌%}
{%如果不是user.is_身份验证%}
+
{%elif post.pk不在已投票%}
+
{%else%}
-
{%endif%}
{%endform%}
您将函数编写为get\u query\u set
,但名称为。此外,您忘记在此处调用get\u queryset(..)
函数:
class PostVoteCountManager(models.Manager):
def get_queryset(self):
return super(PostVoteCountManager, self).get_queryset().annotate(
votes=Count('vote')).order_by("-votes")
类PostVoteCountManager(models.Manager):
def get_queryset(自我):
返回super(PostVoteCountManager,self).get_queryset().annotate(
投票数=计数('vote'))。按(“-vots”)排序。
您没有在返回超级(PostVoteCountManager,self)中调用get\u queryset
。get\u queryset.annotate(…)
,它应该是返回超级(PostVoteCountManager,self)。get\u queryset()。annotate(…)
。