ORM for Python

Sabanova Rubabe Sabanova Rubabe on Oct 3, 2020 · 3 min read ·  Open post in medium.com

Before giving information about some ORMs for Python, it would be better to talk about an ORM concept in general.

So what is an ORM(Object Relational Mapping) ?

The logic behind Object Relational Mapping is being able to write queries and manipulating data using an object-oriented paradigm. It gives an opportunity to deal with a database by using our language (which in this article we will mention Python) instead of SQL. It means that ORMs allows us to create, read, update and delete data and schemas in the database with Python code.

What are the advantages of using ORMs?

What are the disadvantages of using ORMs?

As we already are more aware of an ORM concept, we can move on to some examples of ORM for Python.

The Python ORMs that are mostly used these days are :(I have also added their original documentations for whom that is interested)

The Django ORM
SQLAlchemy
Peewee
PonyORM
SQLObject

Django ORM

The Django web framework has its own built-in object-relational mapping module that can be used to deal with database written in SQLite, PostgreSQL, MySQL and so on. It is also possible not to use this default ORM for Django web framework in particular but others as SQLAlchemy because though Django ORM helps somehow, there are complains much that using Django ORM is mostly harder than just writing the codes in raw SQL.

SQLAlchemy

SQLAlchemy is mostly considered as the best one among other ORMs beacuse of its simplicity, speed and also some other features that other ORMs don’t have. One of the advantages it has is SQLAlchemy allows writing Python code to map data from the database to the applications’ Python objects. No SQL is required to create, maintain and query the database. It provides us working with Python objects instead of writing bridge code to get data in and out of relational tables.

SQLAlchemy can be used with Flask, Pyramid( which they are Python frameworks), Django(though Django doesn’t support it officially yet, there are some ways to switch to SQLAlchemy).

Peewee

Peewee is simpler and easier than most ORMs as Django ORM or SQLAlchemy which makes it useful for the ones who is just getting started web development. Peewee can be used with nearly any web framework which is good news to hear. But at the end of the day because the analogy used by the core Peewee author is that Peewee is to SQLAlchemy as SQLite is to PostgreSQL which makes it hard to use it in big projects.

Pony ORM

Pony also looks like other Python ORMs that is available but it also has differences as Pony makes it much easier to write even so complex SQL codes in pythonic way, also in terms of speed, Pony is one of the best among them. In general we can say that Pony supporting nearly everything that other ORMs have makes it powerful but maybe the biggest problem Pony has is it doesn’t support migration yet (but Pony developers have confirmed they are working on this).

Resourses :

  1. ORM Concepthttps://blog.bitsrc.io/what-is-an-orm-and-why-you-should-use-it-b2b6f75f5e2a
  2. Python ORMs https://www.fullstackpython.com/object-relational-mappers-orms.html
  3. Pony ORM — http://jakeaustwick.me/why-you-should-give-ponyorm-a-chance/