简体   繁体   English

如何在Django中的一个查询中计算一个字段和相同但已过滤的字段?

[英]How to count a field and this same field but filtered, in one query in Django?

I'm really not a web developper so sorry for the title of my question that doesn't really make sense. 我确实不是网络开发人员,所以对我的问题的标题没有任何意义感到抱歉。

To make things easier, here are what my models look like (I stripped non necessary fields for the demonstration): 为了使事情变得容易,这是我的模型的样子(我去除了演示中不必要的字段):

class User(models.Model):
    username = models.CharField(max_length=100)
class Vote(models.Model):
    user = models.ForeignKey(AskFmUser)
    date = models.DateTimeField('vote date')

And in a view, I need to list all users, with the number of votes they received, ordered by votes. 从某种角度来说,我需要列出所有用户,并以投票的顺序列出他们获得的投票数。 I have no problem doing this, with this Query : 我对此没有疑问,使用Query:

global_votecount = Vote.objects.all().values('user', 'user__username').annotate(votesTotal=Count('user__username')).order_by('-votesTotal')

And in my template, I display it on a table with two columns (username, and vote count). 在我的模板中,我将其显示在具有两列(用户名和投票数)的表上。

But what I need to do, is display the total vote count they received all time, but also, in this same HTML table, the name of votes they received today . 但是,我需要做的是显示他们一直收到的总票数,而且还要在同一HTML表中显示他们今天收到的票名。

I know I can get votes for today with this query: 我知道我今天可以通过此查询获得投票:

now = timezone.now()
today_votecount = Vote.objects.filter(date__year=now.year, date__month=now.month, date__day=now.day).values('user', 'user__username').annotate(todayVotesCount=Count('user__username')) 

But the problem is that if no votes were made for a specific user during the day, he would not appear in the list. 但是问题在于,如果白天没有为特定用户投票,那么该用户将不会出现在列表中。 Also, the list, even if ordered with an order_by, would be ordered differently. 同样,即使使用order_by进行排序,列表的排序也将不同。

So... do you see any "clean" solution to be able to get the total vote count and the vote count for a timeperiod (by filtering in the "date" field), in a single query ? 那么...您是否看到任何“干净”的解决方案能够在单个查询中获得总投票数和一个时间段的投票数(通过在“日期”字段中进行过滤)? If not possible... could I manually go though these two ValuesQuerySet to add a field in the first one (corresponding to "today votes count", when a username is found in the second one ? 如果不可能的话...我可以手动通过这两个ValuesQuerySet在第一个字段中添加一个字段(对应于“今天的投票计数”,如果在第二个字段中找到了用户名)?

Thank you 谢谢

I'm not sure how to do it in one query, but consider something like this: 我不确定如何在一个查询中执行此操作,但是请考虑以下内容:

global_votecount = Vote.objects.all().values('user', 'user__username').annotate(votesTotal=Count('user__username')).order_by('-votesTotal')

import datetime
today_min = datetime.datetime.combine(datetime.date.today(), datetime.time.min)
today_max = datetime.datetime.combine(datetime.date.today(), datetime.time.max)

votesToday = dict(Vote.objects.filter(date__range=(today_min, today_max)).values_list('user__username').annotate(votesToday=Count('user__username')))


for item in global_votecount:
    item['votesToday'] = votesToday[item['user__username']]

This uses two queries, and takes the results of the second and applies it to the results of the first query. 这使用两个查询,并获取第二个查询的结果并将其应用于第一个查询的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM