[Python] Django ValueError: Cannot query "instructor": Must be "User" instance

Discussão em 'Python' iniciado por Stack, Maio 7, 2025.

  1. Stack

    Stack Membro Participativo

    I'm getting the following error when trying to access my homepage:

    ValueError at /
    Cannot query "instructor": Must be "User" instance.
    Request Method: GET
    Request URL: http://127.0.0.1:8000/
    Django Version: 5.2
    Exception Type: ValueError
    Exception Value: Cannot query "instructor": Must be "User" instance.
    Exception Location: C:\Users\User LENOVO\OneDrive\Bureau\E-Learning\venv\Lib\site-
    packages\django\db\models\sql\query.py, line 1346, in check_query_object_type
    Raised during: base.views.Accueille


    class User(AbstractUser):
    instructor = models.BooleanField(default=False)
    # ... other fields

    class Course(models.Model):
    title = models.CharField(max_length=255)
    description = models.TextField()
    instructor = models.ForeignKey(User, on_delete=models.CASCADE, related_name='courses')
    # ... other fields
    class Home(View):
    def get(self, request):
    try:
    courses = Course.objects.select_related('instructor').all()
    # ... rest of the view

    Continue reading...

Compartilhe esta Página