我正在评估并考虑将CherryPy用于一个项目,该项目基本上是一个从客户端(浏览器)到后端与Python web服务对话的JavaScript前端。所以,我真的需要一些快速和轻量级的在后端,我可以使用Python实现,然后通过ORM (JSON到浏览器)与PostgreSQL DB对话。

我也在看Django,我喜欢它,因为它的ORM是内置的。然而,我认为Django可能比我真正需要的多一点(即比我真正需要的功能多==更慢?)

谁有使用不同的Python ORM解决方案的经验,可以比较和对比它们的特性和功能、速度、效率等?


当前回答

到目前为止,我们将Elixir与SQLAlchemy一起使用。Elixir在SQLAlchemy上添加了一个层,使其看起来更像“ActiveRecord模式”的计数器部分。

其他回答

到目前为止,我们将Elixir与SQLAlchemy一起使用。Elixir在SQLAlchemy上添加了一个层,使其看起来更像“ActiveRecord模式”的计数器部分。

SQLAlchemy的声明性扩展在0.5中将成为标准,它提供了一个非常类似于Django或Storm的all in one接口。它还与使用datamapper样式配置的类/表无缝集成:

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()

Storm拥有最简单的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()

我想你可以看看:

秋天

风暴

如果你正在寻找轻量级的,并且已经熟悉django风格的声明式模型,请查看pewee: https://github.com/coleifer/peewee

例子:

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')

查看文档以获得更多示例。