1、fields字段的简单使用
示例代码:
import json
from datetime import datetime
from flask_restful import fields, marshal
# 基本例子
resource_fields = {
'name': fields.String,
'address': fields.String,
'date_updated': fields.DateTime(dt_format='rfc822'),
}
class UserInfo(object):
def __init__(self, name, address, date_updated=datetime.now()):
self.name = name
self.address = address
self.date_updated = date_updated
print(UserInfo('dgw', 'beijing'))
print(UserInfo('dgw', 'beijing').name, UserInfo('dgw', 'beijing').address)
print(json.dumps(marshal(UserInfo('dgw', 'beijing'), resource_fields)))
运行结果:
2、fields字段起别名
示例代码:
import json
from datetime import datetime
from flask_restful import fields, marshal
class UserInfo(object):
def __init__(self, name, address, date_updated=datetime.now()):
self.name = name
self.address = address
self.date_updated = date_updated
# 输出域别名
resource_fields2 = {
'open_name': fields.String(attribute='name'),
'open_name2': fields.String, # 如果获取不到该名称对应的属性名,将返回空值
'address': fields.String,
'date_updated': fields.DateTime(dt_format='rfc822'),
}
print(json.dumps(marshal(UserInfo('dgw', 'beijing'), resource_fields2)))
运行结果:
3、fields字段输出默认值
示例代码:
import json
from datetime import datetime
from flask_restful import fields, marshal
# # 输出域默认值
class UserInfo2(object):
def __init__(self, address):
self.address = address
resource_fields3 = {
'open_name': fields.String(default='add_dgw'),
'address': fields.String,
'date_updated': fields.DateTime(dt_format='rfc822', default=str(datetime.now())),
}
print(json.dumps(marshal(UserInfo2(address='beijing'), resource_fields3)))
运行结果:
4、fields字段自定义输出域
示例代码:
import json
from datetime import datetime
from flask_restful import fields, marshal
# 自定义输出域
class UserInfo2(object):
def __init__(self, address, flag, date_updated=datetime.now()):
self.address = address
self.date_updated = date_updated
self.flags = flag
class UrgentItem(fields.Raw):
def format(self, value):
return "Urgent" if value & 0x01 else "Normal"
resource_fields4 = {
'open_name': fields.String(default='add_DGW'),
'address': fields.String,
'date_updated': fields.DateTime(dt_format='rfc822'),
'priority': UrgentItem(attribute='flags'),
}
print(json.dumps(marshal(UserInfo2(address='beijing', flag=0), resource_fields4)))
print(json.dumps(marshal(UserInfo2(address='beijing', flag=1), resource_fields4)))
运行结果:
5、fields字段嵌套List类型
示例代码:
import json
from datetime import datetime
from flask_restful import fields, marshal
# 基本例子
resource_fields = {
'name': fields.String,
'address': fields.String,
'teachers': fields.List(fields.String),
'date_updated': fields.DateTime(dt_format='rfc822'),
}
class UserInfo(object):
def __init__(self, name, address, teachers, date_updated=datetime.now()):
self.name = name
self.address = address
self.teachers = teachers
self.date_updated = date_updated
print(UserInfo('dgw', 'beijing', ['AAA', 'BBB']))
print(UserInfo('dgw', 'beijing', ['AAA', 'BBB']).name, UserInfo('dgw', 'beijing', ['AAA', 'BBB']).teachers)
print(json.dumps(marshal(UserInfo('dgw', 'beijing', ['AAA', 'BBB']), resource_fields, envelope=False)))
运行结果:
6、fields字段嵌套Nested类型
示例代码:
import json
from datetime import datetime
from flask_restful import fields, marshal
class TeacherInfo(object):
def __init__(self, name, age, sex):
self.name = name
self.age = age
self.sex = sex
teacher_fields = {
'name': fields.String,
'age': fields.String,
'sex': fields.String
}
class UserInfo(object):
def __init__(self, name, address, teachers, date_updated=datetime.now()):
self.name = name
self.address = address
self.teachers = teachers
self.date_updated = date_updated
user_fields = {
'name': fields.String,
'address': fields.String,
'teachers': fields.Nested(TeacherInfo.teacher_fields),
'date_updated': fields.DateTime(dt_format='rfc822'),
}
aa = json.dumps(marshal(TeacherInfo('AAA', '26', 'man'), TeacherInfo.teacher_fields))
print(aa)
teacher1 = marshal(TeacherInfo('AAA', '26', 'man'), TeacherInfo.teacher_fields)
teacher2 = marshal(TeacherInfo('BBB', '25', 'woman'), TeacherInfo.teacher_fields)
bb = json.dumps(marshal(UserInfo('dgw', 'beijing', [teacher1, teacher2]), UserInfo.user_fields))
print(bb)
运行结果:
7、fields字段中的复杂结构
示例代码:
import json
from flask_restful import fields, marshal
# 复杂结构
resource_fields = {'name': fields.String}
resource_fields['address'] = {}
resource_fields['address']['line 1'] = fields.String(attribute='addr1')
resource_fields['address']['line 2'] = fields.String(attribute='addr2')
resource_fields['address']['city'] = fields.String
resource_fields['address']['state'] = fields.String
resource_fields['address']['zip'] = fields.String
data = {'name': 'bob', 'addr1': 'shanghai', 'addr2': '', 'city': 'New York', 'state': 'NY', 'zip': '10468'}
print(json.dumps(marshal(data, resource_fields)))
运行结果:
8、fields字段中的嵌套域
示例代码:
import json
from flask_restful import fields, marshal
# 嵌套域
address_fields = {}
address_fields['line 1'] = fields.String(attribute='addr1')
address_fields['line 2'] = fields.String(attribute='addr2')
address_fields['city'] = fields.String(attribute='city')
address_fields['state'] = fields.String(attribute='state')
address_fields['zip'] = fields.String(attribute='zip')
resource_fields = {}
resource_fields['name'] = fields.String
resource_fields['billing_address'] = fields.Nested(address_fields)
resource_fields['shipping_address'] = fields.Nested(address_fields)
address1 = {'addr1': 'qingdao', 'city': 'New York', 'state': 'NY', 'zip': '10468'}
address2 = {'addr1': 'nanjing', 'city': 'New York', 'state': 'NY', 'zip': '10468'}
address3 = {'line 1': 'wuhan', 'line 2': 'tianmen', 'city': 'New York', 'state': 'NY', 'zip': '10468'}
address4 = {'addr1': 'wuhan', 'addr2': 'tianmen', 'city2': 'New York', 'state': 'NY2', 'zip': '11002'}
data1 = {'name': 'bob', 'billing_address': address1, 'shipping_address': address2}
data2 = {'name': 'bob', 'billing_address': address3, 'shipping_address': address4}
print(json.dumps(marshal(data1, resource_fields)))
print(json.dumps(marshal(data2, resource_fields)))
运行结果: