6

When using django, I believe you can swap out the built-in orm for sqlalchemy (not sure how though?).

Are they both basically the same thing or there is a clear winner between the 2?

Blankman
  • 259,732
  • 324
  • 769
  • 1,199
  • You can't use contrib apps with SqlAlchemy as far as I know though you could probably work around this using some kind of reflection. And to use SqlAlchemy in Django just use it in your views as you would any other Python module. – Davor Lucic Sep 13 '10 at 13:52

2 Answers2

8

When using django, I believe you can swap out the built-in orm for sqlalchemy (not sure how though?).

You can use SQLAlchemy in your Django applications. That doesn't mean you can "swap" out the ORM though. Some of Django's built-in batteries would cease to work if you completely replace Django's ORM with SQLAlchemy. For instance the Admin app wouldn't work.

I have read about people using both. Django's ORM to get the batteries to work and SQLAlchemy to tackle complex SQL relationships and queries.

Are they both basically the same thing or there is a clear winner between the 2?

They are not the same thing. SQLAlchemy is more versatile than Django's ORM. For instance while Django's ORM tightly couples the business logic layer and the persistence layer into models, SQLAlchemy will let you keep them separate.

On the other hand SQLAlchemy has a steeper learning curve and would be an overkill for many projects. The existing migration tools for SQLAlchemy may not easily integrate with Django.

All said, I wouldn't presume to declare either a "winner". They are broadly similar tools but with their own strengths, weaknesses and best-fit situations.

While you are on the subject it wouldn't hurt to read this (dated but relevant) blog post about the short comings of the Django ORM and how it compares with SQLAlchemy.

Community
  • 1
  • 1
Manoj Govindan
  • 72,339
  • 21
  • 134
  • 141
0

SQLAlchemy is more powerful, but also more complex, than Django ORM.

Elixir provides an interface on top of SQLAlchemy that is closer to Django ORM in terms of complexity, but also lets you use constructs from SQLAlchemy if Elixir alone isn't enough. For example, I've found SQLAlchemy's AssociationProxy class to be useful on several occasions. You just drop an AssociationProxy field into an Elixir model class and you're good to go.

Nathan Davis
  • 5,636
  • 27
  • 39