我有很多 .annotate 有点复杂的 django 查询:
query = ModuleEngagement.objects.filter(course_id=course_id)\
.values('username')\
.annotate(
videos_overall=Sum(Case(When(entity_type='video', then='count'), output_field=IntegerField()))) \
.annotate(
videos_last_week=Sum(Case(When(entity_type='video', created__gt=seven_days_ago, then=1),
output_field=IntegerField()))) \
.annotate(
problems_overall=Sum(Case(When(entity_type='problem', then='count'), output_field=IntegerField()))) \
.annotate(
problems_last_week=Sum(Case(When(entity_type='problem', created__gt=seven_days_ago, then='count'),
output_field=IntegerField()))) \
.annotate(
correct_problems_overall=Sum(Case(When(entity_type='problem', event='completed', then='count'),
output_field=IntegerField()))) \
.annotate(
correct_problems_last_week=Sum(Case(When(entity_type='problem', event='completed',
created__gt=seven_days_ago, then='count'),
output_field=IntegerField()))) \
.annotate(
problems_attempts_overall=Sum(Case(When(entity_type='problem', event='attempted', then='count'),
output_field=IntegerField()))) \
.annotate(
problems_attempts_last_week=Sum(Case(When(entity_type='problem', event='attempted',
created__gt=seven_days_ago, then='count'),
output_field=IntegerField()))) \
.annotate(
forum_posts_overall=Sum(Case(When(entity_type='discussion', then='count'),
output_field=IntegerField()))) \
.annotate(
forum_posts_last_week=Sum(Case(When(entity_type='discussion', created__gt=seven_days_ago, then='count'),
output_field=IntegerField()))) \
.annotate(
date_last_active=Max('created'))
Run Code Online (Sandbox Code Playgroud)
annotate 是否接受字典作为参数,以便我可以将所有注释移动到其中?如果是这样,语法是什么?
annotate 是否接受字典作为参数,以便我可以将所有注释移动到其中?如果是这样,语法是什么?
您可以执行字典解包。所以,如果你有一个像这样的字典:
my_dict = {
'total_likes': Sum('likes'),
'total_articles': Sum('articles'),
}Run Code Online (Sandbox Code Playgroud)
你可以有一个查询,如:
MyModel.objects.annotate(**my_dict)Run Code Online (Sandbox Code Playgroud)
这相当于:
MyModel.objects.annotate(total_likes=Sum('likes'), total_articles=Sum('articles'))Run Code Online (Sandbox Code Playgroud)
如果您使用django-2.0或更新版本,那么您可以通过以下方式显着简化您的注释:
ModuleEngagement.objects.filter(course_id=course_id).values('username').annotate(
videos_overall=Count('pk', filter=Q(entity_type='video')),
videos_last_week=Count('pk', filter=Q(entity_type='video', created__gt=seven_days_ago)),
problems_overall=Count('pk', filter=Q(entity_type='problem'),
problems_last_week=Count('pk', filter=Q(entity_type='problem', created__gt=seven_days_ago)),
correct_problems_overall=Count('pk', filter=Q(entity_type='problem', event='completed'),
correct_problems_last_week=Count('pk', filter=Q(entity_type='problem', event='completed', created__gt=seven_days_ago)),
problems_attempts_overall=Count('pk', filter=Q(entity_type='problem', event='attempted'),
problems_attempts_last_week=Count('pk', filter=Q(entity_type='problem', event='attempted', created__gt=seven_days_ago)),
forum_posts__overall=Count('pk', filter=Q(entity_type='discussion'),
forum_posts__last_week=Count('pk', filter=Q(entity_type='discussion', event='attempted', created__gt=seven_days_ago)),
date_last_active=Max('created')
).order_by('username')Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1551 次 |
| 最近记录: |