首页 > 代码库 > python flask model 序列化
python flask model 序列化
class DictSerializable(object):
def as_dict(self,*args):
result = OrderedDict()
keys=args or self.__mapper__.c.keys()
for key in keys:
if hasattr(self,key):
obj=getattr(self, key)
if isinstance(obj,basestring):
value=http://www.mamicode.com/obj
elif isinstance(obj,int) or isinstance(obj,long) or isinstance(obj,float):
value=http://www.mamicode.com/obj
elif isinstance(obj,Decimal):
value=http://www.mamicode.com/str(obj)
elif isinstance(obj,types.choice.Choice):
value=http://www.mamicode.com/obj.code
elif isinstance(obj,datetime):
value=http://www.mamicode.com/obj.strftime(‘%Y-%m-%d %M:%H:%S‘)
elif isinstance(obj,date):
value=http://www.mamicode.com/obj.strftime(‘%Y-%m-%d‘)
else:
raise Exception(‘不支持系列化[%s]类型!‘ % type(obj))
result[key] = value
return result
def as_json(self,*args):
d=self.as_dict(*args)
def as_dict(self,*args):
result = OrderedDict()
keys=args or self.__mapper__.c.keys()
for key in keys:
if hasattr(self,key):
obj=getattr(self, key)
if isinstance(obj,basestring):
value=http://www.mamicode.com/obj
elif isinstance(obj,int) or isinstance(obj,long) or isinstance(obj,float):
value=http://www.mamicode.com/obj
elif isinstance(obj,Decimal):
value=http://www.mamicode.com/str(obj)
elif isinstance(obj,types.choice.Choice):
value=http://www.mamicode.com/obj.code
elif isinstance(obj,datetime):
value=http://www.mamicode.com/obj.strftime(‘%Y-%m-%d %M:%H:%S‘)
elif isinstance(obj,date):
value=http://www.mamicode.com/obj.strftime(‘%Y-%m-%d‘)
else:
raise Exception(‘不支持系列化[%s]类型!‘ % type(obj))
result[key] = value
return result
def as_json(self,*args):
d=self.as_dict(*args)
return json.dumps(d)
------------------------------------------------------------------------------------------------------
result1=pagination(page,rows1)
for credit in result1:
rows.append(credit.as_dict(‘id‘,‘title‘,‘week_rule‘,‘latefee_rate‘,‘extra_cost_rate‘,‘due_days‘,‘remark‘))
return json.dumps(credit_result)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。