Python ORM 框架 quick_orm

BSD
Python
跨平台
2012-04-21
tylerlong

quick_orm 是一个 Python 的 ORM 框架. 安装和使用超级简单, 可以一分钟上手! 基于SQLAlchemy构建, 在功能上是SQLAlchemy的超集. quick_orm 最大的特点是以非常简单易学易用的形式提供了完备强大的功能.

 

安装

pip install quick_orm

 


Hello World 示例

from quick_orm.core import Database
from sqlalchemy import Column, String

__metaclass__ = Database.DefaultMeta

class User:
    name = Column(String(30))

Database.register()

if __name__ == '__main__':
    db = Database('sqlite://') # database urls: http://docs.sqlalchemy.org/en/latest/core/engines.html#database-urls
    db.create_tables() # create tables, you don't have to write any SQL.

    user = User(name = 'Hello World')
    db.session.add_then_commit(user) # commit user to database.

    user = db.session.query(User).get(1)
    print 'My name is', user.name
    print 'created_at', user.created_at # created_at and updated_at timestamps are added automatically.
    print 'updated_at', user.updated_at

    user.name = 'Tyler Long'
    db.session.commit() # commit changes to database.
    print 'My name is', user.name
    print 'created_at', user.created_at
    print 'updated_at', user.updated_at

 


问答网站数据建模 示例

from quick_orm.core import Database
from sqlalchemy import Column, String, Text

__metaclass__ = Database.DefaultMeta

class User:
    name = Column(String(70))

@Database.many_to_one(User)
class Post:
    content = Column(Text)

class Question(Post):
    title = Column(String(70))

@Database.many_to_one(Question)
class Answer(Post):
    pass

@Database.many_to_one(Post)
class Comment(Post):
    pass

@Database.many_to_many(Post)
class Tag:
    name = Column(String(70))

Database.register()


#以上是建模部分, 下面是如何使用数据模型


if __name__ == '__main__':
    db = Database('sqlite://')
    db.create_tables()

    user1 = User(name = 'Tyler Long')
    user2 = User(name = 'Peter Lau')

    tag1 = Tag(name = 'quick_orm')
    tag2 = Tag(name = 'nice')

    question = Question(user = user1, title = 'What is quick_orm?', content = 'What is quick_orm?', tags = [tag1, ])
    question2 = Question(user = user1, title = 'Have you tried quick_orm?', content = 'Have you tried quick_orm?', tags = [tag1, ])

    answer = Answer(user = user1, question = question, tags = [tag1, ],
        content = 'quick_orm is a Python ORM framework which enables you to get started in less than a minute!')

    comment1 = Comment(user = user2, content = 'good question', post = question)
    comment2 = Comment(user = user2, content = 'nice answer', post = answer, tags = [tag2, ])

    db.session.add_all_then_commit([question, question2, answer, comment1, comment2, tag1, tag2, ])

    question = db.session.query(Question).get(1)
    print 'tags for question "{0}": "{1}"'.format(question.title, ', '.join(tag.name for tag in question.tags))
    print 'new comment for question:', question.comments.first().content
    print 'new comment for answer:', question.answers.first().comments.first().content

    user = db.session.query(User).filter_by(name = 'Peter Lau').one()
    print 'Peter Lau has posted {0} comments'.format(user.comments.count())

    tag = db.session.query(Tag).filter_by(name = 'quick_orm').first()
    print '{0} questions are tagged "quick_orm"'.format(tag.questions.count())
加载中

评论(1)

MrMign
MrMign
有用过的吗?说说 杂样啊?和storm比? quick_orm

quick_orm 0.4.2 发布了, 兼容SQLAlchemy 0.7.7

quick_orm 0.4.2 发布了, 针对刚刚发布的SQLAlchemy 0.7.7做了全方位的兼容性测试, 没有发现任何问题. quick_orm 是一个 Python 的 ORM 框架. 安装和使用超级简单, 可以一分钟上手! 基于SQL...

2012/05/06 10:36

quick_orm 0.4.0 发布,Python的ORM框架

quick_orm 0.4.0 发布,该版本新增了建模帮助函数 many_to_one 和 one_to_one, 数据建模更加直观简单. quick_orm 是一个 Python 的 ORM 框架. 安装和使用超级简单, 可以一分钟上手! 基于SQL...

2012/04/27 14:29

没有更多内容

加载失败,请刷新页面

没有更多内容

4
回答
"我们的开源项目"活动深圳站本周六下午2:00开始啦

活动背景 Background 真正的开源开发者,其实都是非常务实和低调的。但...... 有些事,你不说,别人怎么能知道呢,让我们大声的讲出来吧。 本次...

2012/06/25 10:56

没有更多内容

加载失败,请刷新页面

没有更多内容

暂无博客

返回顶部
顶部