本文整理汇总了Python中django.db.models.aggregates.Count方法的典型用法代码示例。如果您正苦于以下问题:Python aggregates.Count方法的具体用法?Python aggregates.Count怎么用?Python aggregates.Count使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.db.models.aggregates
的用法示例。
在下文中一共展示了aggregates.Count方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_count
# 需要导入模块: from django.db.models import aggregates [as 别名]
# 或者: from django.db.models.aggregates import Count [as 别名]
def get_count(self, using):
"""
Performs a COUNT() query using the current filter constraints.
"""
obj = self.clone()
obj.add_annotation(Count('*'), alias='__count', is_summary=True)
number = obj.get_aggregation(using, ['__count'])['__count']
if number is None:
number = 0
return number
示例2: get_count
# 需要导入模块: from django.db.models import aggregates [as 别名]
# 或者: from django.db.models.aggregates import Count [as 别名]
def get_count(self, using):
"""
Perform a COUNT() query using the current filter constraints.
"""
obj = self.clone()
obj.add_annotation(Count('*'), alias='__count', is_summary=True)
number = obj.get_aggregation(using, ['__count'])['__count']
if number is None:
number = 0
return number
示例3: all_hot_dicussions
# 需要导入模块: from django.db.models import aggregates [as 别名]
# 或者: from django.db.models.aggregates import Count [as 别名]
def all_hot_dicussions(request):
discussions = Discuss.objects.annotate(reply_num=Count('replys')).all()[:10]
discussions = sorted(discussions, key=lambda x: x.reply_num, reverse=True)
context = {
'discussions': discussions,
}
return render(request, 'Discussion/hot_discussions.html', context=context)
示例4: get_hot_discussions
# 需要导入模块: from django.db.models import aggregates [as 别名]
# 或者: from django.db.models.aggregates import Count [as 别名]
def get_hot_discussions(num=5):
"""
得到指定数量的热门讨论
:param num: 想要得到的讨论数量
:return: Discuss模型的实例列表
"""
discussions = Discuss.objects.annotate(reply_num=Count('replys')).all()
discussions = sorted(discussions, key=lambda x: x.reply_num, reverse=True)
return discussions[:num]
示例5: get_tag_list
# 需要导入模块: from django.db.models import aggregates [as 别名]
# 或者: from django.db.models.aggregates import Count [as 别名]
def get_tag_list():
"""返回标签列表"""
return Tag.objects.annotate(total_num=Count('article')).filter(total_num__gt=0)
# 返回活跃的友情链接查询集
示例6: items
# 需要导入模块: from django.db.models import aggregates [as 别名]
# 或者: from django.db.models.aggregates import Count [as 别名]
def items(self):
return Category.objects.annotate(total_num=Count('article')).filter(total_num__gt=0)
示例7: get_category
# 需要导入模块: from django.db.models import aggregates [as 别名]
# 或者: from django.db.models.aggregates import Count [as 别名]
def get_category():
category_list = Category.objects.annotate(post_num=Count('post')).order_by('-post_num')
Category.objects.values()
return category_list
示例8: get_tag
# 需要导入模块: from django.db.models import aggregates [as 别名]
# 或者: from django.db.models.aggregates import Count [as 别名]
def get_tag():
tag_list = Tag.objects.annotate(post_num=Count('post')).order_by('-post_num')
return tag_list
示例9: get_comment_rank
# 需要导入模块: from django.db.models import aggregates [as 别名]
# 或者: from django.db.models.aggregates import Count [as 别名]
def get_comment_rank(num=5):
app_model = settings.COMMENT_ENTRY_MODEL.split('.')
Post = apps.get_model(*app_model)
post_list = Post.objects.annotate(comment_num=Count('comment')).order_by('-comment_num')
return post_list[:num]
示例10: show_categories
# 需要导入模块: from django.db.models import aggregates [as 别名]
# 或者: from django.db.models.aggregates import Count [as 别名]
def show_categories(context):
category_list = Category.objects.annotate(num_posts=Count('post')).filter(num_posts__gt=0)
return {
'category_list': category_list,
}
示例11: show_tags
# 需要导入模块: from django.db.models import aggregates [as 别名]
# 或者: from django.db.models.aggregates import Count [as 别名]
def show_tags(context):
tag_list = Tag.objects.annotate(num_posts=Count('post')).filter(num_posts__gt=0)
return {
'tag_list': tag_list,
}
示例12: _get_points_earned_trials_count
# 需要导入模块: from django.db.models import aggregates [as 别名]
# 或者: from django.db.models.aggregates import Count [as 别名]
def _get_points_earned_trials_count(self):
"""Get points earned and trials count from the sequence.
:return tuple([trials_count, points_earned])
"""
items_result = self.sequence.items.aggregate(
points_earned=Sum('score'), trials_count=Count('score')
)
return items_result['trials_count'], items_result['points_earned']
示例13: _get_points_earned_trials_count
# 需要导入模块: from django.db.models import aggregates [as 别名]
# 或者: from django.db.models.aggregates import Count [as 别名]
def _get_points_earned_trials_count(self):
"""Get points earned and trials count from the sequence.
:return tuple([trials_count, points_earned])
"""
# Note(idegtiarov) With the first non-problem activity in the sequence and default value of the threshold
# item_result returns None, 0 which are not appropriate for the grade calculation method, valid default values
# are provided to fix this issue.
items_result = self.sequence.items.exclude(is_problem=False).aggregate(
points_earned=Coalesce(Sum('score'), 0), trials_count=Greatest(Count('score'), 1)
)
return items_result['trials_count'], items_result['points_earned']
示例14: get_toolcates
# 需要导入模块: from django.db.models import aggregates [as 别名]
# 或者: from django.db.models.aggregates import Count [as 别名]
def get_toolcates():
'''获取所有工具分类,只显示有工具的分类'''
return ToolCategory.objects.annotate(total_num=Count('toollink')).filter(total_num__gt=0)
示例15: get_tag_list
# 需要导入模块: from django.db.models import aggregates [as 别名]
# 或者: from django.db.models.aggregates import Count [as 别名]
def get_tag_list():
'''返回标签列表'''
return Tag.objects.annotate(total_num=Count('article')).filter(total_num__gt=0)