[英]How to exclude null values in queryset for charts.js
I am trying to get the count of infringements in the market place for the logged in user.我正在尝试计算已登录用户在市场上的侵权行为。 The logged user is part of a group.
登录的用户是组的一部分。 The problem I am having is its still counting values for marketplace items that doesn't belong to the group.
我遇到的问题是它仍在计算不属于该组的市场项目的价值。 It adds 0 in the queryset breaking my charts,
它在查询集中添加 0 打破了我的图表,
u = request.user.groups.all()[0].id
mar_count = Marketplace.objects.annotate(infringement_count=Count('infringement', filter=Q(groups=u)))
The result 'Ebay','Amazon','Facebook','Wallmart', '0','0','0','0','1','1','1','1',结果 'Ebay','Amazon','Facebook','Wallmart', '0','0','0','0','1','1','1','1',
I should be getting 'Ebay','Amazon','Facebook','Wallmart', '1','1','1','1',我应该得到 'Ebay','Amazon','Facebook','Wallmart', '1','1','1','1',
How do I exclude counting the marketplace when its no part of the logged in users group?当它不属于登录用户组时,如何排除计算市场? I am new.
我是新的。 Thanks
谢谢
You can filter out the Marketplace
s, so not in the .annotate(..)
clause [Django-doc] :你可以过滤掉
Marketplace
s,所以不在.annotate(..)
子句 [Django-doc] 中:
u = request.user.groups.all()[0].id
mar_count = Marketplace.objects.filter(groups=u).annotate(
infringement_count=Count('infringement')
)
The count will always be one (or zero if ingrigment
is None
).计数将始终为一(如果
ingrigment
为None
则为零)。
One of the problems with your code snippet is that it will always only work with the first group of that user, and that can be any of the groups the user is a member of, so it is not very consistent.您的代码片段的一个问题是它始终只适用于该用户的第一组,并且可以是该用户所属的任何组,因此它不是很一致。 If you want to count all groups the
user
is a member of, you can use:如果要计算
user
所属的所有组,可以使用:
mar_count = Marketplace.objects.filter(groups__user=request.user).annotate(
infringement_count=Count('infringement')
)
Here the count will always be the number of "matching" groups, or 0
if the infrigment
is NULL
.这里的计数将始终是“匹配”组的数量,如果错误是
infrigment
NULL
0
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.