0

我正在尝试为测验创建一个高分统计表/列表,该表/列表应该显示对要被猜测的人的正确猜测的百分比(或总数)。为了进一步阐述,这些是使用的模型。

测验模型:

class Quiz(models.Model):
participants = models.ManyToManyField(
    User,
    through="Participant",
    through_fields=("quiz", "correct_user"),
    blank=True,
    related_name="related_quiz",
)
fake_users = models.ManyToManyField(User, related_name="quiz_fakes")
user_quizzed = models.ForeignKey(
    User, related_name="user_taking_quiz", on_delete=models.CASCADE, null=True
)
time_started = models.DateTimeField(default=timezone.now)
time_end = models.DateTimeField(blank=True, null=True)
final_score = models.IntegerField(blank=True, default=0)

这个模型也有一些属性;我认为它们与手头的问题无关。

参与者模型:

class Participant(models.Model):  # QuizAnswer FK -> QUIZ
guessed_user = models.ForeignKey(
    User, on_delete=models.CASCADE, related_name="clicked_in_quiz", null=True
)
correct_user = models.ForeignKey(
    User, on_delete=models.CASCADE, related_name="solution_in_quiz", null=True
)
quiz = models.ForeignKey(
    Quiz, on_delete=models.CASCADE, related_name="participants_in_quiz"
)

@property
def correct(self):
    return self.guessed_user == self.correct_user

为了迭代我正在尝试做的事情,我将尝试解释我认为这应该如何工作:

  1. 对于 a Userin User.objects.all(),找到等于(来自模型)participant的所有对象user.idcorrect_userparticipant
  2. 对于每个participant对象,评估 if correct_user==guessed_user
  3. participant将上述比较True针对 的每个对象求和User,由字段表示sum_of_correct_guesses
  4. 返回一个查询集,包括所有带参数的用户 [ User, sum_of_correct_guesses]

^现在理想情况下应该是percentage_of_correct_guesses,但这是事后的想法,应该很容易通过sum_of_correct_guesses对该人的猜测进行 / 和 n 次来改变。

现在我什至为一个人制作了一些伪代码,以向自己大致说明它应该如何使用 python 算法工作

# PYTHON PSEUDO QUERY ---------------------
person = get_object_or_404(User, pk=3)  # Example-person
y = Participant.objects.filter(
    correct_user=person
)  # Find participant-objects where person is used as guess
y_corr = []  # empty list to act as "queryset" in for-loop

for el in y:  # for each participant object
    if el.correct:  # if correct_user == guessed_user
        y_corr.append(el)  # add to queryset
y_percentage_corr = len(y_corr) / len(y)  # do arithmetic division
print("Percentage correct: ", y_percentage_corr)  # debug-display
# ---------------------------------------------

我尝试过的(到目前为止没有成功)是将 ExtensionWrapper 与Count()对象Q一起使用:

percentage_correct_guesses = ExpressionWrapper(
Count("pk", filter=Q(clicked_in_quiz=F("id")), distinct=True)
/ Count("solution_in_quiz"),
output_field=fields.DecimalField())

all_users = (
User.objects.all().annotate(score=percentage_correct_guesses).order_by("score"))

非常感谢有关如何执行此操作的任何帮助或资源指示:))

4

1 回答 1

0

我在四处寻找相关问题时找到了答案: Django 1.11 Annotating a Subquery Aggregate

我所做的是:

  • 创建一个过滤器,其中 anOuterRef()指向 aUser并检查是否与 和User相同correct_person以及之间的比较guessed_person,在查询集中为过滤器接受的所有元素correct_person输出一个值。correct_user
  • correct_user对过滤后的查询集中出现 a 的次数进行带注释的计数。
  • User基于 annotated-count 进行注释,这是真正驱动整个操作的注释。注意如何OuterRef()Subquery用于告诉过滤器应该是哪个用户correct_user

下面是我让它工作的代码片段,它看起来与上面链接问题中的答案非常相似:

from django.db.models import Count, OuterRef, Subquery, F, Q

crit1 = Q(correct_user=OuterRef('pk'))
crit2 = Q(correct_user=F('guessed_user'))
compare_participants = Participant.objects.filter(crit1 & crit2).order_by().values('correct_user')
count_occurrences = compare_participants.annotate(c=Count('*')).values('c')
most_correctly_guessed_on = (
    User.objects.annotate(correct_clicks=Subquery(count_occurrences))
    .values('first_name', 'correct_clicks')
    .order_by('-correct_clicks')
)
return most_correctly_guessed_on

感谢Oli ,这非常有效。

于 2021-12-18T19:30:00.537 回答