14

I am having this error TypeError: 'StudentSubjectGrade' object is not subscriptable of course the data filtered is exist in the database, and i am sure that the filter is correct. what should i do to correct this ?

note: this is recycle question, please dont mind the comment below,

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

this is my traceback

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

2 Answers2

28
TypeError: 'StudentSubjectGrade' object is not subscriptable

this means that student is not a dictionary, you cannot use student['key'] to get what you want.

you should use student.sth instead.

ha-neul
  • 3,058
  • 9
  • 24
  • This only works when you know the name of the field. How about when the name is contained in a variable? – Uche Ozoemena Sep 13 '22 at 11:09
  • For anyone else wondering, you can use `getattr` when you don't know the name of the field, such as in [this answer](https://stackoverflow.com/a/16618872/7987987). – Uche Ozoemena Sep 13 '22 at 11:29
7

Use getattr(student, 'key') instead; with 'key' e.g 'id'