码迷,mamicode.com
首页 > 编程语言 > 详细

python flask model 序列化

时间:2014-06-18 06:25:55      阅读:596      评论:0      收藏:0      [点我收藏+]

标签:python   flask   数据库   

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=obj
                elif isinstance(obj,int) or isinstance(obj,long) or isinstance(obj,float):
                    value=obj
                elif isinstance(obj,Decimal):
                    value=str(obj)
                elif isinstance(obj,types.choice.Choice):
                    value=obj.code
                elif isinstance(obj,datetime):
                    value=obj.strftime(‘%Y-%m-%d %M:%H:%S‘)
                elif isinstance(obj,date):
                    value=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)




python flask model 序列化,布布扣,bubuko.com

python flask model 序列化

标签:python   flask   数据库   

原文地址:http://blog.csdn.net/fanyunlei/article/details/31775133

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!