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 (i.e. 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.
import datetimefrom peewee import*classBlog(Model):
name =CharField()classEntry(Model):
blog =ForeignKeyField(Blog)
title =CharField()
body =TextField()
pub_date =DateTimeField(default=datetime.datetime.now)# query it like djangoEntry.filter(blog__name='Some great blog')# or programmatically for finer-grained controlEntry.select().join(Blog).where(Blog.name =='Some awesome blog')
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')
I usually use SQLAlchemy. It’s pretty powerful and is probably the most mature python ORM.
If you’re planning on using CherryPy, you might also look into dejavu as it’s by Robert Brewer (the guy that is the current CherryPy project leader). I personally haven’t used it, but I do know some people that love it.
SQLObject is a little bit easier to use ORM than SQLAlchemy, but it’s not quite as powerful.
Personally, I wouldn’t use the Django ORM unless I was planning on writing the entire project in Django, but that’s just me.
SQLAlchemy’s declarative extension, which is becoming standard in 0.5, provides an all in one interface very much like that of Django or Storm. It also integrates seamlessly with classes/tables configured using the datamapper style:
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()
We use Elixir alongside SQLAlchemy and have liked it so far. Elixir puts a layer on top of SQLAlchemy that makes it look more like the “ActiveRecord pattern” counter parts.
There is no conceivable way that the unused features in Django will give a performance penalty. Might just come in handy if you ever decide to upscale the project.
I used Storm + SQLite for a small project, and was pretty happy with it until I added multiprocessing. Trying to use the database from multiple processes resulted in a “Database is locked” exception. I switched to SQLAlchemy, and the same code worked with no problems.
It’s really easy to use and the models you work with aren’t bad at all. Django uses SQLAlchemy for it’s ORM but using it by itself lets you use it’s full power.
Here’s a small example on creating and selecting orm objects