[Python] flask_restful fields
Python Flask api 앱을 만드는 도중 marshal_with 와 fields 라이브러를 이용해
output 데이터 타입을 설정할 떄 찾은 팁 공유
아래와 같이 사용하면 특정한 타입으로 응답필드 값을 설정하여 사용할 수 있다.
from flask_restful import fields
class MyDateFormat(fields.Raw):
def format(self, value):
return value.strftime('%Y-%m-%d')
resource_fields = {
'id': fields.Integer,
'date': MyDateFormat
}
참조
Custom date format for flask_restful fields.DateTime for marshal_with
I am using flask_restful's fields and marshal_with to serialize my APIs output data. But I am having problems with my date format. I am storing the date as an SQLAlchemy DateTime type, for example,...
stackoverflow.com
https://flask-restplus.readthedocs.io/en/stable/marshalling.html
Response marshalling — Flask-RESTPlus 0.13.0 documentation
Response marshalling Flask-RESTPlus provides an easy way to control what data you actually render in your response or expect as in input payload. With the fields module, you can use whatever objects (ORM models/custom classes/etc.) you want in your resourc
flask-restplus.readthedocs.io