Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I start with Django ORM to easily switch to SQLAlchemy?

Like Pinterest and others I want to make great apps in Django. Many of them are talking about limitations of Django ORM - Pinterest are using only one table in Django ORM. I think it can be User table or django_session. It is because they had started with Django ORM.

So, if I am going develop and promote my site for a long time, what solution should I choose? I am going to stay with django, and not switch to flask or nodeJS.

Some django experts are modifying Djagno ORM, sessions, etc. e.g. https://github.com/devhub/baph.

But is it a bit risky for me and I can't use many of Django features like sessions, reset password. However, some great apps work also with SqlAlchemy like: django-tastypie.

But of course the database is the most important, and it must easily evolve to always be fast.

So what solution should I use? Is it no problem to use Django ORM for the User and django session table? For the rest I will use sqlAlchemy. So I can use django-registration, sessions, etc.

Can you see any problems in this solution?

like image 393
user2721435 Avatar asked Aug 27 '13 12:08

user2721435


People also ask

Does Django use SQLAlchemy as ORM?

Main difference is that Django ORM uses the “active record implementation”, and SQL Alchemy uses “data mapper implementation”. It means that Django ORM cannot use our models for queries if every row is not linked with the overall model object.

Is SQLAlchemy better than Django ORM?

In addition to this, since it interacts directly with the database, you can simply run the queries against the database without actually using the ORM. Plus, SQLAlchemy is much more powerful than Django, albeit with a little higher learning curve.

How do you use ORM in Django?

ORM is an acronym for the object-relational mapper. The ORM's main goal is to transmit data between a relational database and application model. The ORM automates this transmission, such that the developer need not write any SQL. ORM, as from the name, maps objects attributes to respective table fields.

Does Django have a built in ORM?

One of the most powerful features of Django is its Object-Relational Mapper (ORM), which enables you to interact with your database, like you would with SQL.


1 Answers

There is kind of a general rule that Django works fine as long as you do it the Django way. In this context it means that you will most likely run into some unexpected problems and have a lot of effort if you want to use SQLAlchemy inside of Django.

Django's ORM is fine. It is not as good as SQLAlchemy (personal opinion) but it get's the job done very well. So my recommendation is: Unless you have a very good reason to use SQLAlchemy and are sure it is worth the effort, just use the Django ORM for everything.

However, if there are good reasons to not use it, then you can replace it and work with SQLAlchemy. As you already described, leaving default Django tables in the ORM will save you some of the pain involved here, so that is a good idea, too.

And finally, Django might not even be your best solution. If you already know Django and know that it is what you want then use it. Otherwise, you could also look around if there are no other frameworks you like better (in the near range to Django I only know of Pyramid, for smaller apps a microframework might suffice).

Edit: To accomodate your comments, here are some more details:

  • Could you share the connections? I think this should be possible, but you would have to create a connection pool for SQLAlchemy that uses the Django ORM connections (or possibly create your own pool and write adapters for both, not so sure how this works with Django). However, I think you'd still have to give out distinct connections, because you'd never want one interferring with the other.
  • Not using the generic Django helpers (admin, forms, etc.): If you don't care about the generics you take a lot of pain out of replacing the ORM. So this sounds like a sane way to go and if you didn't want them in the first place you are not loosing anything here.
  • Memory usage: There's not much you can do here, but I wouldn't worry about this too much. If memory really is a concern you wouldn't use Django to begin with.
like image 125
javex Avatar answered Oct 12 '22 22:10

javex