Seventy-nine: flask.Restful the flask-Restful standardized return parameter example

 

Connect a code and data

 

For complex data structure definition data only if there is no sense of a single structure, then returned, and this time the data structure definition to be accurate to each field of all the data
the data structure sometimes to return, there will be more complex data structures after confirmed that you can use some special fields to achieve, if the value of a field is a list, then use fields.List, if the value of a field is a dictionary, use fields.Nested

from flask import Flask
import config
from exts import db
from models import User, Article, Tag
from flask_restful import Api, Resource, fields, marshal_with

app = Flask(__name__)
app.config.from_object(config)
db.init_app(app)
api = Api(app)


class ArticleView(Resource):
resource_fields = {
'title': fields.String,
'content': fields.String,
'author': fields.String,
'tags': fields.String
}

@marshal_with(resource_fields)
def get(self, article_id):
article = Article.query.get(article_id)
return article


api.add_resource(ArticleView, '/article/<article_id>/', endpoint='article')

 

Use fields.List specified data and nested fields.Nested

class ArticleView(Resource):
# 定义要返回的数据结构
resource_fields = {
'title': fields.String, # article.title
'content': fields.String,
'author': fields.Nested({ # article.author
'username': fields.String, # article.author.username
'email': fields.String # article.author.email
}),
'tags': fields.List( # article.tags的list
fields.Nested({ # article.tags[n]
'id': fields.Integer, # # article.tags[n].id
'name': fields.String # # article.tags[n].name
})
)
}

 

Renaming a property
often facing the user name of the field names are different from the code field, the use of such a modified attribute may be provided in the mapping field names, such as: 'article_title': fields.String (attribute = 'title'), article_title actually returned value article.title field

class ArticleView(Resource):
# 定义要返回的数据结构
resource_fields = {
'article_title': fields.String(attribute='title'), # article.title
'content': fields.String,
'author': fields.Nested({ # article.author
'username': fields.String, # article.author.username
'email': fields.String # article.author.email
}),
'tags': fields.List( # article.tags的list
fields.Nested({ # article.tags[n]
'id': fields.Integer, # # article.tags[n].id
'name': fields.String # # article.tags[n].name
})
),
}

默认值:
在返回一些字段的时候,有时候没有值,那么可以指定fields的时候指定一个默认值,如:'count': fields.Integer(default=30)

class ArticleView(Resource):
# 定义要返回的数据结构
resource_fields = {
'article_title': fields.String(attribute='title'), # article.title
'content': fields.String,
'author': fields.Nested({ # article.author
'username': fields.String, # article.author.username
'email': fields.String # article.author.email
}),
'tags': fields.List( # article.tags的list
fields.Nested({ # article.tags[n]
'id': fields.Integer, # # article.tags[n].id
'name': fields.String # # article.tags[n].name
})
),
'count': fields.Integer(default=30)
}

Guess you like

Origin www.cnblogs.com/zhongyehai/p/11877169.html