我试图在django查询集中创建注释。注释是基于条件的反向外键计数。我遇到的问题是,当我用一个条件计算一个反向外键时,我得到了正确的数据,但是当我做两个注释时,每个反向外键一个注释。你知道吗
以下是一个反向外键的带有计数注释的查询集:
ExamResponse.objects.filter(
course_class__course=course,
exam__exam_type=Exam.PRACTICE,
user__is_demo=False,
ended__isnull=False,
id=125752
).order_by(
'user_id',
'started'
).annotate(
total_ecq_count=Sum(
Case(
When(
choice_questions__response_time__gte=ENGAGEMENT_THRESHOLD,
choice_questions__id__isnull=False,
then=1
),
default=0,
output_field=IntegerField()
),
distinct=True
),
).values('total_ecq_count')
结果(正确结果):
<QuerySet [{'total_ecq_count': 1}]>
带有两个计数注释的查询
ExamResponse.objects.filter(
course_class__course=course,
exam__exam_type=Exam.PRACTICE,
user__is_demo=False,
ended__isnull=False,
id=125752
).order_by(
'user_id',
'started'
).annotate(
total_ecq_count=Sum(
Case(
When(
choice_questions__response_time__gte=ENGAGEMENT_THRESHOLD,
choice_questions__id__isnull=False,
then=1
),
default=0,
output_field=IntegerField()
),
distinct=True
),
total_etq_count=Sum(
Case(
When(
text_questions__response_time__gte=ENGAGEMENT_THRESHOLD,
text_questions__id__isnull=False,
then=1
),
default=0,
output_field=IntegerField()
),
distinct=True
),
).values('total_ecq_count', 'total_etq_count')
结果:(ecq总数由1增加到3!!!)你知道吗
<QuerySet [{'total_ecq_count': 3, 'total_etq_count': 4}]>
我刚刚发现这篇文章,显然,这是一个已知的错误,两个不同的外键的两个注释创建重复计数。https://code.djangoproject.com/ticket/25861
相关问题 更多 >
编程相关推荐