我也正在看Django,我很喜欢它,因为它的ORM已经内置了。但是,我认为Django可能比我实际需要的更多一些功能(即我不需要的功能会导致速度变慢?)。
请问有没有人对不同的Python ORM解决方案有经验,并可以比较和对比它们的功能、速度、效率等方面?
import datetime
from peewee import *
class Blog(Model):
name = CharField()
class Entry(Model):
blog = ForeignKeyField(Blog)
title = CharField()
body = TextField()
pub_date = DateTimeField(default=datetime.datetime.now)
# query it like django
Entry.filter(blog__name='Some great blog')
# or programmatically for finer-grained control
Entry.select().join(Blog).where(Blog.name == 'Some awesome blog')
查看文档以获取更多示例。
ORDER BY
DESC
的说明”如何暗示“不适合活动记录模式”? - jpmc26Storm 可能拥有最简单的API:
from storm.locals import *
class Foo:
__storm_table__ = 'foos'
id = Int(primary=True)
class Thing:
__storm_table__ = 'things'
id = Int(primary=True)
name = Unicode()
description = Unicode()
foo_id = Int()
foo = Reference(foo_id, Foo.id)
db = create_database('sqlite:')
store = Store(db)
foo = Foo()
store.add(foo)
thing = Thing()
thing.foo = foo
store.add(thing)
store.commit()
而且当你需要时,它使得进入原始SQL变得轻松无痛:
store.execute('UPDATE bars SET bar_name=? WHERE bar_id like ?', [])
store.commit()
我通常使用 SQLAlchemy。它非常强大,可能是最成熟的Python ORM。
如果你打算使用CherryPy,你也可以看一下Dejavu,因为它是由Robert Brewer(目前的CherryPy项目负责人)开发的。我个人没有使用过它,但我知道有些人很喜欢。
SQLObject比SQLAlchemy更容易使用,但功能不如后者强大。
对于我个人而言,除非打算在Django中编写整个项目,否则我不会使用Django ORM,但这只是我的个人看法。
SQLAlchemy的declarative扩展,在0.5版中正式成为标准,提供了一个类似于Django或Storm的全面接口,与使用数据映射器风格配置的类/表无缝集成:
Base = declarative_base()
class Foo(Base):
__tablename__ = 'foos'
id = Column(Integer, primary_key=True)
class Thing(Base):
__tablename__ = 'things'
id = Column(Integer, primary_key=True)
name = Column(Unicode)
description = Column(Unicode)
foo_id = Column(Integer, ForeignKey('foos.id'))
foo = relation(Foo)
engine = create_engine('sqlite://')
Base.metadata.create_all(engine) # issues DDL to create tables
session = sessionmaker(bind=engine)()
foo = Foo()
session.add(foo)
thing = Thing(name='thing1', description='some thing')
thing.foo = foo # also adds Thing to session
session.commit()
我们使用Elixir和SQLAlchemy一起使用,并且迄今为止很喜欢它。 Elixir在SQLAlchemy的基础上添加了一个层次结构,使其更像“ActiveRecord模式”的对应物。
在Django中,未使用的功能不可能会导致性能损失。如果您决定升级项目,这些功能可能会派上用场。