본문 바로가기
개발/파이썬

Flask의 make_response에서 TypeError: The view function did not return a valid response. The return type must be a string, dict, tuple, Response instance, or WSGI callable, but it was a list.

by 장모 2021. 11. 3.
from flask_marshmallow import Marshmallow

ma = Marshmallow()

class UserSchema(SQLAlchemyAutoSchema):
    class Meta:
        model = User

users = [User(), User(), User(), ...]

data = UserSchema().dump(users, many=True)
res = make_response(data)

 

File "C:\Users\...\flask\app.py", line 2127, in make_response
    raise TypeError(
TypeError: The view function did not return a valid response. The return type must be a string, dict, tuple, Response instance, or WSGI callable, but it was a list.

 

마시멜로우에서 dump를 했을 때 목록이면 결과가 list가 된다. 그리고 make_response는 사전형은 처리할 수 있는데 리스트는 처리하지 못해서 위와 같은 오류가 발생한다.  

 

해결책은 그냥 jsonify를 사용해서 응답을 만들면 된다. 

from flask import jsonify

...
res = jsonify(data)

 

끝. 

 

 

댓글