{"id":182759,"date":"2020-10-31T05:20:45","date_gmt":"2020-10-30T21:20:45","guid":{"rendered":"http:\/\/4563.org\/?p=182759"},"modified":"2020-10-31T05:20:45","modified_gmt":"2020-10-30T21:20:45","slug":"%e8%af%b7%e6%95%99%e9%97%ae%e9%a2%98%e3%80%82flask-%e4%b8%ad%e7%94%a8-sqlalchemy-%e6%9f%a5%e8%af%a2%e5%a4%9a%e8%a1%a8%e5%af%b9%e8%b1%a1%e6%9c%aa%e6%8c%87%e5%ae%9a%e5%88%97%e6%88%96%e5%85%b6%e4%b8%ad","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=182759","title":{"rendered":"\u8bf7\u6559\u95ee\u9898\u3002Flask \u4e2d\u7528 SQLAlchemy \u67e5\u8be2\u591a\u8868\u5bf9\u8c61\u672a\u6307\u5b9a\u5217\u6216\u5176\u4e2d\u4e00\u8868\u672a\u6307\u5b9a\u5217\u65f6\uff0c\u5982\u4f55\u628a\u67e5\u8be2\u7ed3\u679c\u8f6c\u4e3a Json"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u8bf7\u6559\u95ee\u9898\u3002Flask \u4e2d\u7528 SQLAlchemy \u67e5\u8be2\u591a\u8868\u5bf9\u8c61\u672a\u6307\u5b9a\u5217\u6216\u5176\u4e2d\u4e00\u8868\u672a\u6307\u5b9a\u5217\u65f6\uff0c\u5982\u4f55\u628a\u67e5\u8be2\u7ed3\u679c\u8f6c\u4e3a Json               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : luxiaoer <\/span>  <span><i><\/i> 0<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<h1>\u4ee3\u7801\u7247\u6bb5<\/h1>\n<p> <\/p>\n<pre><code># \u8868 class ProductCheckBillMod(Base):     __tablename__ = 'sc_product_check'     id = Column(Integer, primary_key=True)     bill_no = Column(String)  class ProductCheckBillMod(Base):     __tablename__ = 'sc_product_check_detail'     id = Column(Integer, primary_key=True)  # \u67e5\u8be2   task_obj = db.session.query(*DBModels).join(*joins).filter(*filters).order_by(*orderbys).slice((page_index - 1) * page_size,page_index * page_size).all()  <\/code><\/pre>\n<p>\u60c5\u51b5 1 DBModels = [ProductCheckBillMod,ProductCheckBillMod]<\/p>\n<p>\u60c5\u51b5 2 DBModels = [ProductCheckBillMod.id,ProductCheckBillMod.bill_no,ProductCheckBillMod]<\/p>\n<h4>\u5728\u4ee5\u4e0a\u7684\u4e24\u79cd\u60c5\u51b5\u4e0b task_obj \u5982\u4f55\u8f6c json<\/h4>\n<h4>\u4ee5\u4e0b\u8fd9\u6bb5\u67e5\u8be2\u7ed3\u679c\u8f6c dict \u662f Copy \u7684\uff0c\u7a0d\u5fae\u6539\u52a8\u8fc7<\/h4>\n<pre><code>  def queryToDict(models,fsModel=None):     if fsModel == None:         fsModel = Model     if isinstance(fsModel,list):         fsModel=fsModel[0]     if (isinstance(models, list)):         if not len(models):             return []         if (isinstance(models[0], fsModel)):             lst = []             for model in models:                 gen = model_to_dict(model)                 dit = dict((g[0], g[1]) for g in gen)                 lst.append(dit)             return lst         else:             res = result_to_dict(models)             return res     else:         if (isinstance(models, fsModel)):             gen = model_to_dict(models)             dit = dict((g[0], g[1]) for g in gen)             return dit         else:             res = dict(zip(models.keys(), models))             find_datetime(res)             return res  # \u5f53\u7ed3\u679c\u4e3a result \u5bf9\u8c61\u5217\u8868\u65f6\uff0cresult \u6709 key()\u65b9\u6cd5 def result_to_dict(results):     res = [dict(zip(r.keys(), r)) for r in results]     # \u8fd9\u91cc r \u4e3a\u4e00\u4e2a\u5b57\u5178\uff0c\u5bf9\u8c61\u4f20\u9012\u76f4\u63a5\u6539\u53d8\u5b57\u5178\u5c5e\u6027     for r in res:         find_datetime(r)     return res  def model_to_dict(model):  # \u8fd9\u6bb5\u6765\u81ea\u4e8e\u53c2\u8003\u8d44\u6e90     for col in model.__table__.columns:         if isinstance(col.type, DateTime):             value = convert_datetime(getattr(model, col.name))         elif isinstance(col.type, Numeric):             value = float(getattr(model, col.name))         else:             value = getattr(model, col.name)         yield (col.name, value)  def find_datetime(value):     pass  def convert_datetime(value):     pass  <\/code><\/pre>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>5<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"3961313\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : 18870715400 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5e94\u8be5\u4e0d\u662f\u8f6c\u6362\u6210 json \u5e94\u8be5\u662f\u5b57\u5178\u5427<br \/>\u5982\u679c\u662f.all()\u7684\u7ed3\u679c<br \/>[dict((col.name, getattr(obj, col.name)) for col in obj.__table__.columns) for obj in objs]<\/p>\n<p>\u5982\u679c\u662f.first()\u7684\u7ed3\u679c<br \/>dict((col.name, getattr(objs, col.name)) for col in objs.__table__.columns)                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3961314\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : luxiaoer <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @18870715400 <br \/>\u611f\u8c22\u56de\u590d\uff0c\u6211\u67e5\u8be2\u540e\u7684 task_obj \u662f\u4e00\u4e2a result \u5bf9\u8c61\uff0c\u5c1d\u8bd5\u4f7f\u7528\u65f6\u63d0\u793a<br \/>AttributeError: &#8216;result&#8217; object has no attribute &#8216;__table__&#8217;                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3961315\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : 18870715400 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @luxiaoer \u6211 \u4f7f\u7528\u7684\u662f sqlalchemy \u540c\u6837\u90fd\u662f\u8f6c\u6362\u6210 \u5b57\u5178, \u4f60\u53ef\u4ee5 dir \u4e00\u4e0b\u770b\u4e00\u4e0b\u5e94\u8be5\u6709\u7c7b\u4f3c\u7684\u65b9\u6cd5, \u6216\u8005\u76f4\u63a5\u53bb\u5b98\u65b9\u6587\u6863\u770b\u4e00\u4e0b\u5e94\u8be5\u6709<br \/>https:\/\/www.cnblogs.com\/eating-gourd\/p\/9997751.html \u53c2\u8003                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3961316\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : luxiaoer <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @18870715400 \u662f\u7684\uff0c\u6211\u7684\u6838\u5fc3\u4ee3\u7801\u90e8\u5206\u5c31\u662f copy \u7684\u8fd9\u90e8\u5206\u3002<br \/>\u5df2\u89e3\u51b3                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3961317\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : luxiaoer <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u56de\u8f66\u5feb\u4e86\uff0c\u8d34\u4e0b\u89e3\u51b3\u4ee3\u7801\u90e8\u5206<\/p>\n<p>\u91cd\u5199\u4e86 result_to_dict \u51fd\u6570\uff0c\u5e0c\u671b\u53ef\u4ee5\u5e2e\u5230\u5176\u4ed6\u5c0f\u4f19\u4f34<br \/># \u5f53\u7ed3\u679c\u4e3a result \u5bf9\u8c61\u5217\u8868\u65f6\u4e00\u822c\u4e3a\u591a\u8868\u6216\u591a\u5217\u67e5\u8be2\u7ed3\u679c<br \/>def result_to_dict(results):<br \/> res=[]<br \/> for result in results:<br \/> row_d = dict()<br \/> for attr in dir(result):<br \/> if(not attr.startswith(&#8216;_&#8217;) and attr not in [&#8216;count&#8217;,&#8217;index&#8217;,&#8217;keys&#8217;]):<br \/> value = getattr(result, attr)<br \/> if &#8220;_sa_instance_state&#8221; in dir(value):# \u901a\u8fc7 dir \u4e2d\u662f\u5426\u542b\u6709_sa_instance_state \u5224\u5b9a\u662f\u5426\u4e3a Model \u7c7b<br \/> gen = model_to_dict(value)<br \/> dit = dict((g[0], g[1]) for g in gen)<br \/> row_d.update(dit) # \u66f4\u65b0\u5230\u884c\u5b57\u5178\u4e2d<br \/> else:<br \/> row_d[attr] = getattr(result, attr) # \u901a\u8fc7 getattr \u83b7\u53d6\u503c\u6dfb\u52a0\u5230\u5b57\u5178<br \/> res.append(d)<br \/> for r in res:<br \/> find_datetime(r)<br \/> return res                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u8bf7\u6559\u95ee\u9898\u3002Flask \u4e2d\u7528 SQL&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/182759"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=182759"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/182759\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=182759"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=182759"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=182759"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}