What are some good Python ORM solutions?
I'm evaluating and looking at using CherryPy for a project that's basically a JavaScript front-end from the client-side (browser) that talks to a Python web service on the back-end. So, I really need something fast and lightweight on the back-end that I can implement using Python that then speaks to the PostgreSQL DB via an ORM (JSON to the browser).
I'm also looking at Django, which I like, since its ORM is built-in. However, I think Django might be a little more than I really need (ie more features than I really need == slower?).
Anyone have any experience with different Python ORM solutions that can compare and contrast their features and functionality, speed, efficiency, etc.?
SQLAlchemy is more full-featured and powerful (uses the DataMapper pattern). Django ORM has a cleaner syntax and is easier to write for (ActiveRecord pattern). I don't know about performance differences.
SQLAlchemy also has a declarative layer that hides some complexity and gives it a ActiveRecord-style syntax more similar to the Django ORM.
I wouldn't worry about Django being "too heavy." It's decoupled enough that you can use the ORM if you want without having to import the rest.
That said, if I were already using CherryPy for the web layer and just needed an ORM, I'd probably opt for SQLAlchemy.
If you're looking for lightweight and are already familiar with django-style declarative models, check out peewee: https://github.com/coleifer/peewee
Example:
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')
Check the docs for more examples.
Storm has arguably the simplest 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()
And it makes it painless to drop down into raw SQL when you need to:
store.execute('UPDATE bars SET bar_name=? WHERE bar_id like ?', [])
store.commit()
链接地址: http://www.djcxy.com/p/17188.html