在处理查询集时,有没有办法绕过“Field 'id' Expected a number”?

问题描述 投票:0回答:1

我正在开发一个学习平台。我希望能够跟踪用户完成章节的进度。我创建了一个新模型并使用表单将其插入到 html 中。然而,当我提交时,我不断收到 TypeError ,具体信息为“Field 'id'期望一个数字,但得到了

我尝试将用于识别用户、学生和章节的所有变量转换为其主键,但出现了更复杂的错误。如果有更简单的方法来跟踪进度或教程,请分享。

Environment:


Request Method: POST
Request URL: http://192.168.0.118:8000/learn/jerome/mathematics/addition/using-you-fingers

Django Version: 5.0.6
Python Version: 3.12.1
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'core',
 'learn']
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']



Traceback (most recent call last):
  File "C:\Users\KAMBAFWILE JOSEPH\OneDrive\Documents\Web\django\learn\.venv\Lib\site-packages\django\db\models\fields\__init__.py", line 2117, in get_prep_value
    return int(value)
           ^^^^^^^^^^

The above exception (int() argument must be a string, a bytes-like object or a real number, not 'QueryDict') was the direct cause of the following exception:
  File "C:\Users\KAMBAFWILE JOSEPH\OneDrive\Documents\Web\django\learn\.venv\Lib\site-packages\django\core\handlers\exception.py", line 55, in inner
    response = get_response(request)
               ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\KAMBAFWILE JOSEPH\OneDrive\Documents\Web\django\learn\.venv\Lib\site-packages\django\core\handlers\base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\KAMBAFWILE JOSEPH\OneDrive\Documents\Web\django\learn\.venv\Lib\site-packages\django\contrib\auth\decorators.py", line 23, in _wrapper_view
    return view_func(request, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\KAMBAFWILE JOSEPH\OneDrive\Documents\Web\django\learn\kptt\learn\views.py", line 109, in chapter_dashboard
    form = StudentProgressForm(
           
  File "C:\Users\KAMBAFWILE JOSEPH\OneDrive\Documents\Web\django\learn\kptt\learn\forms.py", line 26, in __init__
    student_queryset = Student.objects.filter(user=user, student_slug=student_slug)
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\KAMBAFWILE JOSEPH\OneDrive\Documents\Web\django\learn\.venv\Lib\site-packages\django\db\models\manager.py", line 87, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\KAMBAFWILE JOSEPH\OneDrive\Documents\Web\django\learn\.venv\Lib\site-packages\django\db\models\query.py", line 1476, in filter
    return self._filter_or_exclude(False, args, kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\KAMBAFWILE JOSEPH\OneDrive\Documents\Web\django\learn\.venv\Lib\site-packages\django\db\models\query.py", line 1494, in _filter_or_exclude
    clone._filter_or_exclude_inplace(negate, args, kwargs)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\KAMBAFWILE JOSEPH\OneDrive\Documents\Web\django\learn\.venv\Lib\site-packages\django\db\models\query.py", line 1501, in _filter_or_exclude_inplace
    self._query.add_q(Q(*args, **kwargs))
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\KAMBAFWILE JOSEPH\OneDrive\Documents\Web\django\learn\.venv\Lib\site-packages\django\db\models\sql\query.py", line 1613, in add_q
    clause, _ = self._add_q(q_object, self.used_aliases)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\KAMBAFWILE JOSEPH\OneDrive\Documents\Web\django\learn\.venv\Lib\site-packages\django\db\models\sql\query.py", line 1645, in _add_q
    child_clause, needed_inner = self.build_filter(
                                 
  File "C:\Users\KAMBAFWILE JOSEPH\OneDrive\Documents\Web\django\learn\.venv\Lib\site-packages\django\db\models\sql\query.py", line 1559, in build_filter
    condition = self.build_lookup(lookups, col, value)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\KAMBAFWILE JOSEPH\OneDrive\Documents\Web\django\learn\.venv\Lib\site-packages\django\db\models\sql\query.py", line 1389, in build_lookup
    lookup = lookup_class(lhs, rhs)
             ^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\KAMBAFWILE JOSEPH\OneDrive\Documents\Web\django\learn\.venv\Lib\site-packages\django\db\models\lookups.py", line 30, in __init__
    self.rhs = self.get_prep_lookup()
               ^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\KAMBAFWILE JOSEPH\OneDrive\Documents\Web\django\learn\.venv\Lib\site-packages\django\db\models\fields\related_lookups.py", line 156, in get_prep_lookup
    self.rhs = target_field.get_prep_value(self.rhs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\KAMBAFWILE JOSEPH\OneDrive\Documents\Web\django\learn\.venv\Lib\site-packages\django\db\models\fields\__init__.py", line 2119, in get_prep_value
    raise e.__class__(
    ^^^^^^^^

Exception Type: TypeError at /learn/jerome/mathematics/addition/using-you-fingers
Exception Value: Field 'id' expected a number but got <QueryDict: {'csrfmiddlewaretoken': ['YMvOO6ZYGWVVfxfgoFLVEanZ9zK70CrqlRIx5Y2LOkbzH8Mx3UHPlQYczqLbq1Qt'], 'chapter': ['2'], 'completed': ['on'], 'student': ['1']}>.

这是我尝试过的

这是他的学生进步模型

class StudentProgress(models.Model):
    student = models.ForeignKey(
        Student, on_delete=models.CASCADE, related_name="student_progress"
    )
    chapter = models.ForeignKey(
        Chapter, on_delete=models.CASCADE, related_name="student_progress"
    )
    completed = models.BooleanField(default=False)

    

这是表格。某些代码仅选择当前登录的用户及其正在显示的页面的章节。

class StudentProgressForm(forms.ModelForm):
    class Meta:
        model = StudentProgress
        fields = ("chapter", "completed", "student")
        # widgets = {
        #     "student": forms.HiddenInput(),
        # }

    def __init__(self, user, student_slug, chapter_slug, *args, **kwargs):
        super(StudentProgressForm, self).__init__(*args, **kwargs)
        student_queryset = Student.objects.filter(user=user, student_slug=student_slug)

        if student_queryset.exists():
            self.instance.student = student_queryset.first()
        else:
            self.instance.student = None

        if student_queryset.exists():
            self.fields["student"].initial = student_queryset.first()

        current_chapter = Chapter.objects.filter(chapter_slug=chapter_slug).first()
        self.fields["chapter"].initial = current_chapter

    def get_current_chapter(self, current_chapter_slug):
        return Chapter.objects.filter(chapter_slug=current_chapter_slug).first()

这是我在views.py文件中使用的逻辑

    if request.method == "POST":
        form = StudentProgressForm(
            request.POST,
            instance=single_chapter,
            student_slug=student_slug,
            chapter_slug=chapter_slug,
        )
        if form.is_valid():
            form.save()
            return redirect("user_dashboard")

    else:
        form = StudentProgressForm(
            user=request.user,
            instance=single_chapter,
            student_slug=student_slug,
            chapter_slug=chapter_slug,
        )

最后 html 文件具有这种形式

<form method="post">
      {% csrf_token %}
      {{ form.as_p }}
      <button type="submit">Submit</button>
    </form>
django django-models django-views django-forms django-templates
1个回答
0
投票

在POST请求的views.py中,初始化StudentProgressForm时没有指定用户字段。如果不指定

user
字段,当处理表单时
request.POST
表示
QueryDict
不是用户,这会导致错误。

您可以通过修改表单和视图来解决此问题,如下所示:

在学生进度表中:
def __init__(self, data, user, student_slug, chapter_slug, *args, **kwargs):
视图中:
form = StudentProgressForm(
    data=request.POST,
    user=request.user
    instance=single_chapter,
    student_slug=student_slug,
    chapter_slug=chapter_slug,
)

此外,为了减少额外的查询,您可以尝试以下操作:

def __init__(self, data, user, student_slug, chapter_slug, *args, **kwargs):
    super().__init__(data, *args, **kwargs)
    student_queryset = Student.objects.filter(user=user, student_slug=student_slug)
    
    student = student_queryset.first() # 👈🏻 if  student not exists then default well be None
    self.instance.student = student
    self.fields["student"].initial = student

    current_chapter = Chapter.objects.filter(chapter_slug=chapter_slug).first()
    self.fields["chapter"].initial = current_chapter
© www.soinside.com 2019 - 2024. All rights reserved.