18

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
7
  • where are those images located? Commented Sep 24, 2020 at 5:23
  • that image will save in my database and media folder, Commented Sep 24, 2020 at 5:56
  • The student is a model instance and it is not subscriptable Commented Dec 1, 2020 at 13:46
  • What do you mean? Commented Dec 1, 2020 at 14:10
  • please show your models.py Commented Dec 1, 2020 at 16:48

2 Answers 2

33
+50
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.

Sign up to request clarification or add additional context in comments.

2 Comments

This only works when you know the name of the field. How about when the name is contained in a variable?
For anyone else wondering, you can use getattr when you don't know the name of the field, such as in this answer.
10

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

1 Comment

This is usable iteratively for other usages getattr(getattr(getattr(student, 'key'),'key2'),'key3')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.