Introduction to Django ORMS

Introduction to Django ORMS

ยท

2 min read

Hey there ๐Ÿ‘‹ welcome, In this article, I'm going to be demystifying what ORM is in Django. There are two things I'm going to cover in this article: What is ORM? and Why ORM?. And of course, ORM means Object-relational mapper.

The Django web framework includes a default object-relational mapping layer which is the Object-relational mapper. The ORM isn't a Django-specific technology. This is something that you may find in other technologies and other frameworks. For example, if you are utilizing or using Flask (which is also a Python web framework) you might utilize or hook into the SQL alchemy which is another ORM.

ORM in Django or Object Relational Mapping Layer in Django helps you to facilitate interaction between application data, and various relational databases like MySQL, PostgreSQL, SQLite, etc. The ORM comes built-in with the Django Framework and is an implementation of the ORM. django-ORM-vs-SQL.png

To cut all these long stories short, ORM helps us to interact with our database using our language (or framework) of choices instead of SQL.

Why ORM?

  1. Developers write Python code instead of SQL to:

    • Create,
    • Update,
    • Deleted data && Schemas.
  2. In some cases, it helps us in writing less code that relates to one of the Django principles or benefits which is creating applications quickly.

  3. Easy switching of database or migrating of the database. E.g MySQL to PostgreSQL

Also, I just want to quickly draw your attention to something called An Adaptor or Driver. An Adaptor or driver is a transport facility that allows client programs to pass queries to a database e.g Postgres and also to receive results of the queries coming from the database. For example, if you are connecting the Postgresql database to Django, you use Psycopg2 which serves as an Adaptor in your program.

Why not ORM?

  1. SQL provides us the ability to fine-tune queries and enhance performance over what the ORM has.

2.ORM provides extraction, so you from the developer end, might not know what is going on.