下面是小编给大家带来关于编写Python脚本把sqlAlchemy对象转换成dict的教程(共含5篇),一起来看看吧,希望对您有所帮助。同时,但愿您也能像本文投稿人“蒸气盐”一样,积极向本站投稿分享好文章。
作者:wangdai 字体:[增加 减小] 类型:
这篇文章主要介绍了编写Python脚本把sqlAlchemy对象转换成dict的教程,主要是基于Python的model类构建一个转换的方法,需要的朋友可以参考下
在用sqlAlchemy写web应用的时候,经常会用json进行通信,跟json最接近的对象就是dict,有时候操作dict也会比操作ORM对象更为方便,毕竟不用管数据库session的状态了,
假设数据库里有一张post表,其中一种方法就是
p = session.query(Post).firstp.__dict__
但由于p是sqlAlchemy的对象,所以p.__dict__中会有一些其他的属性比如_sa_instance这种我们不需要关注的
那么我们可以给model的基类加一个方法,假设models.py中原来是这样
Base = sqlalchemy.ext.declarative.declarative_base()class Post(Base): __tablename__ = ‘post‘ id = Column(Integer, primary_key=True) title = Column(String)
那么我们可以加一个to_dict()方法到Base类中
def to_dict(self): return {c.name: getattr(self, c.name, None) for c in self.__table__.columns}Base.to_dict = to_dict
这样就可以
p = session.query(Post).first()p.to_dict()
当然,如果model没有和table绑定的话model里是没有__table__的信息的,可能也会出问题,不过我目前觉得这样最方便了
本文章来给大家详细介绍windows下python安装sqlalchemy教程与在安装sqlalchemy之前要准备工具,有需要的朋友可参考消息
如果是windows操作系统,我们需要先安装easy_install,那么我们先来安装easy_install
.安装setuptools(easy_install):
a.如果是32位的windows操作系统
32位的windows操作系统,直接找到对应的exe,然后安装即可
b.如果是64位的windows操作系统
windows 64为下没有安装包,需要自己编译安装,过程如下:
下载ez_setup.py
运行:
代码如下复制代码python ez_setup.py2.然后配置环境变量:
例如我电脑将路径:D:toolsPython27Scripts
添加到环境变量中
--EOF--
安装成功easy_install之后我们再来给python安装sqlalchemy
然后执行下面的命令
代码如下复制代码easy_install SQLAlchemy
显示如下,安装成功:
代码如下复制代码D:>easy_install SQLAlchemy
Searching for SQLAlchemy
Reading pypi.python.org/simple/SQLAlchemy/
Reading www.sqlalchemy.org
Best match: SQLAlchemy 0.8.0b2
Downloading pypi.python.org/packages/source/S/SQLAlchemy/SQLAlchemy-0.8.0
b2.tar.gz#md5=09537c7b425d0be433f7aceabbf23d68
Processing SQLAlchemy-0.8.0b2.tar.gz
Running SQLAlchemy-0.8.0b2setup.py -q bdist_egg --dist-dir c:usersw3appdata
localtempeasy_install-lnagt1SQLAlchemy-0.8.0b2egg-dist-tmp-0flwk1
warning: no files found matching '*.jpg' under directory 'doc'
no previously-included directories found matching 'docbuildoutput'
***************************************************************************
Unable to find vcvarsall.bat
WARNING: The C extension could not be compiled, speedups are not enabled.
Failure information, if any, is above.
Retrying the build without the C extension now.
***************************************************************************
warning: no files found matching '*.jpg' under directory 'doc'
no previously-included directories found matching 'docbuildoutput'
zip_safe flag not set; analyzing archive contents...
***************************************************************************
WARNING: The C extension could not be compiled, speedups are not enabled.
Plain-Python build succeeded.
***************************************************************************
Adding sqlalchemy 0.8.0b2 to easy-install.pth file
Installed d:toolspython27libsite-packagessqlalchemy-0.8.0b2-py2.7.egg
Processing dependencies for SQLAlchemy
Finished processing dependencies for SQLAlchemy
D:>
--EOF--
“ print ”
This page was generated by a Python CGI script.
“ print ”