Tengo este error TypeError: 'StudentSubjectGrade' object is not subscriptable
, por supuesto, los datos filtrados existen en la base de datos, y estoy seguro de que el filtro es correcto. que debo hacer para corregir esto?
nota: esta es una pregunta de reciclaje, no se preocupe por el comentario a continuación,
def SummaryPeriod(request): period = request.GET.get('period') subject = request.GET.get('subject') teacher = request.GET.get('teacher') print(period, "period", "subject", subject) cate = gradingCategories.objects.all() students = StudentSubjectGrade.objects.filter( grading_Period=period).filter( Subjects=subject).filter( Teacher = teacher ) print(students) Categories = list(cate.values_list('id', flat=True).order_by('id')) table = [] student_name = None table_row = None columns = len(Categories) + 1 table_header = ['Student Names'] table_header.extend(list(cate.values('CategoryName', 'PercentageWeight'))) table.append(table_header) for student in students: if not student['Students_Enrollment_Records__Students_Enrollment_Records__Student_Users__Lastname'] + ' ' + \ student[ 'Students_Enrollment_Records__Students_Enrollment_Records__Student_Users__Firstname'] == student_name: if not table_row is None: table.append(table_row) table_row = [None for d in range(columns)] student_name = student[ 'Students_Enrollment_Records__Students_Enrollment_Records__Student_Users__Lastname'] + ' ' + \ student['Students_Enrollment_Records__Students_Enrollment_Records__Student_Users__Firstname'] table_row[0] = student_name id = student['id'] table_row.append(id) table_row[Categories.index(student['Grading_Categories']) + 1] = student['Average'] * student[ 'Grading_Categories__PercentageWeight'] / 100 table.append(table_row) return render(request, 'Homepage/summaryPeriod.html', {'table': table, "teacher": teacher, "subject": subject, "period": period})
este es mi rastreo
Internal Server Error: /SummaryPeriod/ Traceback (most recent call last): File "C:\Users\USER\Desktop\venv\lib\site-packages\django\core\handlers\exception.py", line 47, in inner response = get_response(request) File "C:\Users\USER\Desktop\venv\lib\site-packages\django\core\handlers\base.py", line 179, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "C:\Users\Desktop\Homepage\views.py", line 2693, in SummaryPeriod if not student['Students_Enrollment_Records__Students_Enrollment_Records__Student_Users__Lastname'] + ' ' + \ TypeError: 'StudentSubjectGrade' object is not subscriptable [01/Dec/2020 21:21:01] "GET /SummaryPeriod/?period=3&subject=18&teacher=5 HTTP/1.1" 500 70398
TypeError: 'StudentSubjectGrade' object is not subscriptable
esto significa que student
no es un diccionario, no puede usar student['key']
para obtener lo que desea.
deberías usar student.sth
en su lugar.