[Fixed]-What is the purpose of ORM with code entities in Django?

1👍

ORM stands for Object-Relational Mapping. What ORM does is to map database entities to Python code, usually classes. You construct your queries in Python and the queries get translated to SQL behind the scene.

For example instead of this query:

SELECT * FROM api_users;

We can use:

users = User.objects.all()

If we used the SQL, we would have had to execute that using a driver, parse the results and then access the columns. But the ORM here is doing those for us. We’re getting instances of User class and get the columns like user.first_name. That makes things easier and faster to use.

As mentioned by John Gordon in the comments, one of the benefits of using the ORM is that we can switch the backend database without much worries. The SQL queries vary from one database engine to another. By abstracting the SQL queries, we are not actually hardcoding the SQL queries. The ORM knows how to construct the queries for each db engine.

👤masnun

Leave a comment