Home > Net >  FieldError at /teacher/8/. Cannot resolve keyword 'teacher' into field. Choices are: class
FieldError at /teacher/8/. Cannot resolve keyword 'teacher' into field. Choices are: class

Time:05-06

I reach the error in my django project when I was trying to display my view. The problem seems like related to the Course model but i honestly dont know where to begin with it.

My Teacher model:

class Teacher(models.Model):
    GENDER_MALE = 0
    GENDER_FEMALE = 1
    GENDER_CHOICES = [(GENDER_MALE, 'Male'), (GENDER_FEMALE, 'Female')]
    
    fname = models.TextField(max_length=20)
    lname = models.TextField(max_length=20)
    gender = models.IntegerField(choices=GENDER_CHOICES)
    phone = models.IntegerField(default=None, blank=True, null=True)
    email = models.TextField(max_length=30, default=None, blank=True, null=True)
    faculty = models.ForeignKey('Faculty', on_delete=models.CASCADE )
    
    def __str__(self):
        return self.lname   ' '    self.fname

My Course model:

class Course(models.Model):
    name = models.TextField(max_length=50)
    faculty = models.ForeignKey('Faculty', on_delete=models.CASCADE )
    def __str__(self):
        return f'{self.name}'

My Faculty model:

class Faculty(models.Model):
    name = models.TextField(max_length=30)
    def __str__(self):
        return f'{self.name}'

My view:

def teacher(request, teacher_id):
    teacher = get_object_or_404(Teacher, pk=teacher_id)
    faculties = Faculty.objects.filter(teacher=teacher)
    course = Course.objects.filter(teacher=teacher)
    classrooms = Classroom.objects.filter(teacher=teacher)
    students = Student.objects.filter(teacher=teacher)
    # students = []
    # for cls in classrooms:
    #     students.extend(Student.objects.filter(classroom=cls))
    return render(request, 'polls/teacher.html', {'teacher': teacher,'faculties': faculties, 'courses':course,'classrooms':classrooms, 'students':students})

When i run the website, it point out in my view the code to be stoped at course = Course.objects.filter(teacher=teacher)

My template:

{% extends "polls/base.html" %}
{% block body %}
    <h2>{{ teacher.fname }} {{ teacher.lname }}</h2>

    <h3>Faculty</h3>

    {% if faculties %}
        {% for faculty in faculties %}
            <p><a href="{% url 'faculty' faculty.id %}">{{ faculty.name }}</a></p>
        {% endfor %}
    {% else %}
        <p> </p>
    {% endif %}

    <h3>Course</h3>
    {%if courses %}
        {% for course in courses %}
            <p><a href="{% url 'course' course.id %}">{{ course.name }}</a> </p>
        {% endfor %}
    {% else %}
        <p> </p>
    {% endif %}

    <h3>Class</h3>
    {% if classrooms %}
        {% for class in classrooms %}
            <p><a href="{% url 'classroom' class.id %}">{{ class.name }}</a> </p>
        {% endfor %}
    {% else %}
        <p> </p>
    {% endif %}

    <h3>Student</h3>    
    {% if students %}
        {% for student in students %}
            <p><a href="{% url 'student' student.id %}">{{ student.fname }} {{ student.lname }}</a></p>
        {% endfor %}
    {% else %}
        <p> </p>
    {% endif %}

{% endblock %}

CodePudding user response:

This happens because of the following line:

course = Course.objects.filter(teacher=teacher)

The Course model has no field teacher, so you can't filter by it. To fix it there are two options:

Add a field to Course:

teacher = models.ForeignKey(Teacher, on_delete=models.CASCADE)

and then add a Teacher to every Course that already exists (or make it null=True).

Or you can filter Course by the Faculty that the teacher is a member of, to get all courses that have a teacher that belongs to that faculty:

courses = Course.objects.filter(faculty=teacher.faculty)

Or, using reverse foreign key lookup:

courses = teacher.faculty.course_set.all()
  • Related