This post provides an introduction to the Django ORM, emphasizing its strengths and weaknesses. It covers the basics of object-relational mapping, the reasons for using an ORM, potential pitfalls like lazy loading and N+1 queries, and provides guidance on migrations, querying, and performance improvements. It also details the differences between raw SQL and how Django ORM handles various SQL operations.
Table of contents
What is an ORM (Object Relational Mapper)?Why use an ORM? - ProsWhy use an ORM? - ConsFundamentalsMigrationsQueryingWHEREWHERE across tables?WHERE other conditionalsASSubqueriesCorrelated SubqueriesPerformance improvementsselect_relatedMany-to-many — prefetch_relatedto_attrMultiple instances when filtering across many-to-manyGotchas and other Funky stuffValuesOther optionsUpdating rowsProblems with updatesOther semi-related things (non deterministic ordering)Sort: