19. An introduction to the Object-Relational Mapper
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.
In fact, Django’s ORM is just a pythonical way to create SQL to query and manipulate your database and get results in a pythonic fashion. It’s really clever engineering that takes advantage of some of the more complex parts of Python to make developers’ lives easier.
We can consider some pseudocode which demonstrates how we might query a database, send the answer to result, parse to an array, create a new object instance called this_user and then extract information into a variable called Name:
query = 'SELECT name, age FROM users WHERE uid=15' result = database_handle.send_sql(query) data_array = ParseToArray(result) this_user = new User(data_array) Name = this_user['name']
Django has a class called model which handles all of these operations. The pseudocode in Django would be a lot more terse:
Result = User.get_by_id(object_id) name = result.name
This process of translating objects to database entities and back again is called Object Relationship Mapping or ORM.
This typically works well when objects and single database tables can be easily and conceptually mapped onto one another.
ORM does have some shortcomings:
- Objects are hierarchical, tables are not
- Objects are flexible to model graphs
- Type mismatches between programming languages and RDBMS
- Differing degrees of atomicity
These shortcomings are known as object relational impedance mismatch.
For the vast majority of operations and database designs, these mismatches will likely not be a problem for your web application. You might spend many years building applications before encountering edge cases where object relational impedance mismatch arises.
Wednesday 3 November 2021, 710 views
Next post: 20. ORM – work through example Previous post: 18. Altering the database
Advanced Web Development index
- 38. Writing API tests
- 37. Testing in Django
- 36. Class-based views in the Django REST framework
- 35. Building a RESTful web service in Django
- 34. Introduction to CRUD, REST and APIs
- 33. Refactoring with generic views in Django
- 32. Django validators
- 31. Django forms (2) – using the ModelForm class
- 30. Django forms (1)
- 29. JavaScript basics
- 28. Adding CSS to the template
- 27. Django templating
- 26. Deleting and updating records
- 25. Joins, filters and chaining commands
- 24. Using the ORM in views.py
- 23. Adding to the database by writing a script
- 22. Adding to the database with Django Admin
- 21. Migrations
- 20. ORM – work through example
- 19. An introduction to the Object-Relational Mapper
- 18. Altering the database
- 17. SQL functions and summaries
- 16. SQL Query performance
- 15. Queries and table joins in SQL
- 14. Inserts and queries in SQL
- 13. Good practice in relational database design
- 12. Limitations to database modelling
- 11. Building a database using SQL
- 10. Introduction to PostgreSQL
- 9. How to start writing a new application in Django
- 8. Building a lightweight project
- 7. Django URLs
- 6. Django templates
- 5. Django models
- 4. Django views
- 3. Creating a new hello app
- 2. Creating a new virtual environment
- 1. Setting up Django
Leave a Reply