ValueError: Cannot query "": Must be "" instance

ValueError: Cannot query "": Must be "" instance

我有一个测验应用程序,用户可以根据需要多次尝试每个测验。我现在正尝试在用户个人资料上显示一些测验统计信息。我遇到的困难是显示用户在测验中的平均分数。

所以首先,我得到正确答案的总数:

correct_answers = QuizTaker.objects.filter(user = self.user, completed = True).aggregate(Sum('correct_answers'))['correct_answers__sum']

这按预期工作。现在我需要获取每个测验的尝试次数,并将其乘以每个测验中的问题数。然后我就可以计算平均分了。

我在尝试获取每个测验的尝试次数时遇到了困难。如果我按如下方式对其进行硬编码,它将起作用:

number_attempts = QuizTaker.objects.filter(user = self.user, completed = True, quiz__title='Afrika').aggregate(Max('attempt_number'))['attempt_number__max']

但是我需要每个测验的最大尝试次数。所以我需要做一个 for loop.

quiztakers = QuizTaker.objects.filter(user = self.user, completed = True)
max_attempts = []
        for quiz in quiztakers:
            number_attempts = QuizTaker.objects.filter(user = self.user, completed = True, quiz=quiz).aggregate(Max('attempt_number'))['attempt_number__max']
            attempts.append(number_attempts)
        return max_attempts

但是这给了我以下错误:

ValueError at /accounts/profile/
Cannot query "Username- Quiz: Afrika - Attempt number: 2": Must be "Quiz" instance.

相关机型:

class Quiz(models.Model):
    type_of_content = models.CharField(max_length=1, choices=CONTENT_TYPES)
    song = models.ForeignKey(Song, null=True, on_delete=models.CASCADE)
    title = models.CharField(max_length=15)
    slug = models.SlugField(blank=True)
    questions_count = models.IntegerField(default=0)

class QuizTaker(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    quiz = models.ForeignKey(Quiz, on_delete=models.CASCADE)
    correct_answers = models.IntegerField(default=0)
    completed = models.BooleanField(default=False)
    attempt_number = models.PositiveIntegerField(default=0)
    timestamp = models.DateTimeField(auto_now_add=True)

请注意,attempt_number 是在 views 中计算的,并且按预期工作。

以及完整的回溯:

Environment:


Request Method: GET
Request URL: http://dev-lr:8000/accounts/profile/

Django Version: 3.0.3
Python Version: 3.8.2
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.humanize',
 'rest_framework',
 'bootstrap4',
 'bootstrapform',
 'languages',
 'django_countries',
 'import_export',
 'django_tables2',
 'django_filters',
 'nested_admin',
 'accounts',
 'vocab',
 'flash',
 'api',
 'videos',
 'quizzes',
 'django_cleanup.apps.CleanupConfig']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']


Template error:
In template C:\Users\mvren\OneDrive\Documents\Coding\Russki\mysite\templates\base.html, error at line 0
   Cannot query "Melissavr - Quiz: Afrika - Attempt number: 2": Must be "Quiz" instance.
   1 : <!DOCTYPE html>
   2 : {% load static %}
   3 : <html lang="en" dir="ltr">
   4 :   <head>
   5 :     <meta charset="utf-8">
   6 :     <meta name="viewport" content="width=device-width, initial-scale=1">
   7 :     <title>Linga Russki</title>
   8 : {% comment %} Bootstrap CSS {% endcomment %}
   9 :   <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
   10 : {% comment %} Google fonts {% endcomment %}


Traceback (most recent call last):
  File "C:\Users\mvren\miniconda3\envs\myRuEnv\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "C:\Users\mvren\miniconda3\envs\myRuEnv\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Users\mvren\miniconda3\envs\myRuEnv\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\mvren\OneDrive\Documents\Coding\Russki\mysite\accounts\views.py", line 49, in profile
    return render(request, 'accounts/profile.html', context)
  File "C:\Users\mvren\miniconda3\envs\myRuEnv\lib\site-packages\django\shortcuts.py", line 19, in render
    content = loader.render_to_string(template_name, context, request, using=using)
  File "C:\Users\mvren\miniconda3\envs\myRuEnv\lib\site-packages\django\template\loader.py", line 62, in render_to_string
    return template.render(context, request)
  File "C:\Users\mvren\miniconda3\envs\myRuEnv\lib\site-packages\django\template\backends\django.py", line 61, in render
    return self.template.render(context)
  File "C:\Users\mvren\miniconda3\envs\myRuEnv\lib\site-packages\django\template\base.py", line 171, in render
    return self._render(context)
  File "C:\Users\mvren\miniconda3\envs\myRuEnv\lib\site-packages\django\template\base.py", line 163, in _render
    return self.nodelist.render(context)
  File "C:\Users\mvren\miniconda3\envs\myRuEnv\lib\site-packages\django\template\base.py", line 936, in render
    bit = node.render_annotated(context)
  File "C:\Users\mvren\miniconda3\envs\myRuEnv\lib\site-packages\django\template\base.py", line 903, in render_annotated
    return self.render(context)
  File "C:\Users\mvren\miniconda3\envs\myRuEnv\lib\site-packages\django\template\loader_tags.py", line 150, in render
    return compiled_parent._render(context)
  File "C:\Users\mvren\miniconda3\envs\myRuEnv\lib\site-packages\django\template\base.py", line 163, in _render
    return self.nodelist.render(context)
  File "C:\Users\mvren\miniconda3\envs\myRuEnv\lib\site-packages\django\template\base.py", line 936, in render
    bit = node.render_annotated(context)
  File "C:\Users\mvren\miniconda3\envs\myRuEnv\lib\site-packages\django\template\base.py", line 903, in render_annotated
    return self.render(context)
  File "C:\Users\mvren\miniconda3\envs\myRuEnv\lib\site-packages\django\template\loader_tags.py", line 62, in render
    result = block.nodelist.render(context)
  File "C:\Users\mvren\miniconda3\envs\myRuEnv\lib\site-packages\django\template\base.py", line 936, in render
    bit = node.render_annotated(context)
  File "C:\Users\mvren\miniconda3\envs\myRuEnv\lib\site-packages\django\template\base.py", line 903, in render_annotated
    return self.render(context)
  File "C:\Users\mvren\miniconda3\envs\myRuEnv\lib\site-packages\django\template\base.py", line 986, in render
    output = self.filter_expression.resolve(context)
  File "C:\Users\mvren\miniconda3\envs\myRuEnv\lib\site-packages\django\template\base.py", line 670, in resolve
    obj = self.var.resolve(context)
  File "C:\Users\mvren\miniconda3\envs\myRuEnv\lib\site-packages\django\template\base.py", line 795, in resolve
    value = self._resolve_lookup(context)
  File "C:\Users\mvren\miniconda3\envs\myRuEnv\lib\site-packages\django\template\base.py", line 857, in _resolve_lookup
    current = current()
  File "C:\Users\mvren\OneDrive\Documents\Coding\Russki\mysite\accounts\models.py", line 65, in quiz_stats
    number_attempts = QuizTaker.objects.filter(user = self.user, completed = True, quiz=quiz).aggregate(Max('attempt_number'))['attempt_number__max']
  File "C:\Users\mvren\miniconda3\envs\myRuEnv\lib\site-packages\django\db\models\manager.py", line 82, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "C:\Users\mvren\miniconda3\envs\myRuEnv\lib\site-packages\django\db\models\query.py", line 904, in filter
    return self._filter_or_exclude(False, *args, **kwargs)
  File "C:\Users\mvren\miniconda3\envs\myRuEnv\lib\site-packages\django\db\models\query.py", line 923, in _filter_or_exclude
    clone.query.add_q(Q(*args, **kwargs))
  File "C:\Users\mvren\miniconda3\envs\myRuEnv\lib\site-packages\django\db\models\sql\query.py", line 1350, in add_q
    clause, _ = self._add_q(q_object, self.used_aliases)
  File "C:\Users\mvren\miniconda3\envs\myRuEnv\lib\site-packages\django\db\models\sql\query.py", line 1377, in _add_q
    child_clause, needed_inner = self.build_filter(
  File "C:\Users\mvren\miniconda3\envs\myRuEnv\lib\site-packages\django\db\models\sql\query.py", line 1284, in build_filter
    self.check_related_objects(join_info.final_field, value, join_info.opts)
  File "C:\Users\mvren\miniconda3\envs\myRuEnv\lib\site-packages\django\db\models\sql\query.py", line 1122, in check_related_objects
    self.check_query_object_type(value, opts, field)
  File "C:\Users\mvren\miniconda3\envs\myRuEnv\lib\site-packages\django\db\models\sql\query.py", line 1103, in check_query_object_type
    raise ValueError(

Exception Type: ValueError at /accounts/profile/
Exception Value: Cannot query "Melissavr - Quiz: Afrika - Attempt number: 2": Must be "Quiz" instance.

这不会起作用,因为您正尝试通过 Quiztaker 实例查询,而您应该通过 Quiz 实例查询

    for quiztaker in quiztakers:
        number_attempts = QuizTaker.objects.filter(user=self.user, completed=True, 
            quiz=quiztaker.quiz).aggregate(Max('attempt_number')) 
            ['attempt_number__max']
        attempts.append(number_attempts)
    return max_attempts

这也不是最好的主意,因为您可以通过使用 annotate and values(GROUP BY 语句)

进行适当的查询来优化它