Is there an way to go around the “Field ‘id’ expected a number” when dealing with a queryset?

I am working on a learning platform. I want to be able to track user progress of the chapters completed. I cerated a new model and inserted it into the html using a form. However when I submit, I keep getting a TypeError with the specifics that “Field ‘id’ expected a number but got <QueryDict: {‘csrfmiddlewaretoken’: [‘YMvOO6ZYGWVVfxfgoFLVEanZ9zK70CrqlRIx5Y2LOkbzH8Mx3UHPlQYczqLbq1Qt’], ‘chapter’: [‘2’], ‘completed’: [‘on’], ‘student’: [‘1’]}>

I tried converting all the variables I was using to identify the user, student and chapter to their primary keys but I got even more complicated error. If there is an easier way to track the progress or a tutorial on a way to do it, please share.

Here’s what I tried

This is he StudemtProgress Model

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)

    

This is the form. Some of the code is to pick only the current logged in user and the chapter of th page that it is being displayed.

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()

This is the logic I used in the views.py file

    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,
        )

And lastly the html file has this form

<form method="post">
      {% csrf_token %}
      {{ form.as_p }}
      <button type="submit">Submit</button>
    </form>

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật