代码:
def get_average(questions):
i = []
for question in questions.items:
if question.correct:
i.append(question)
return len(i) * 10
运行时出现错误AttributeError: 'function' object has no attribute 'items'
。 questions
参数传递给flask-sqlalchemy分页对象viewable here。
这应该是一个列表,并且在运行代码时:
for x in questions.items:
print(x.correct)
它正常工作并且符合预期。更改导致此错误的内容。这两段代码都使用相同的questions
对象。
整个视图功能如下:
students = current_user.students
student_averages = {}
for student in students:
student_averages[student.username] = (student, [])
student_avg_list = student_averages[student.username][1]
questions = student.questions.paginate(page=1, per_page=10)
for x in questions.items: # Works
print(x.question_text)
def get_average(questions):
i = []
for question in questions.items: # Gives error
if question.correct:
i.append(question)
return len(i) * 10