4 ms·
The Django ORM is one of my favourite abstractions on top of SQL.
by simonw 4y ago
The Django ORM is one of my favourite abstractions on top of SQL.
- rglullis 4y agoNice try, Django core developer... :p
- db48x 4y agoNow now, we all have some biases.
- quantified 4y agoI haven't gotten into Python so its advantages are unknown. Does it provide an object model for join, aggregation, filter and from clauses? Can I compose named functions that represent transforms (map, filter, etc.) in a type-safe fashion with each other and have those translate to appropriate CTEs? ORM has no use for me, very little of what I work on exchanges data with programming-language objects.
- simonw 4y ago> Does it provide an object model for join, aggregation, filter and from clauses? Yes: https://docs.djangoproject.com/en/4.1/topics/db/queries/#retrieving-specific-objects-with-filters https://docs.djangoproject.com/en/4.1/topics/db/queries/#ret... and https://docs.djangoproject.com/en/4.1/topics/db/queries/#lookups-that-span-relationships https://docs.djangoproject.com/en/4.1/topics/db/queries/#loo... > Can I compose named functions that represent transforms (map, filter, etc.) in a type-safe fashion with each other and have those translate to appropriate CTEs The Django ORM doesn't use CTEs (yet). It's common however to create and compose custom filter operations which the ORM will then combine into larger SQL queries. A rough example: def today(qs): return qs.filter(created__date = datetime.date.today()) def tagged(qs, tag): # This becomes a many-to-many lookup return qs.filter(tags__tag = tag) entries = today(tagged(Entry.objects.all(), "python") This would build the following SQL query: SELECT "blog_entry"."id", "blog_entry"."created", "blog_entry"."slug", "blog_entry"."metadata", "blog_entry"."search_document", "blog_entry"."import_ref", "blog_entry"."card_image", "blog_entry"."series_id", "blog_entry"."title", "blog_entry"."body", "blog_entry"."tweet_html", "blog_entry"."extra_head_html", "blog_entry"."custom_template" FROM "blog_entry" INNER JOIN "blog_entry_tags" ON ("blog_entry"."id" = "blog_entry_tags"."entry_id") INNER JOIN "blog_tag" ON ("blog_entry_tags"."tag_id" = "blog_tag"."id") WHERE ( "blog_tag"."tag" = 'python' AND ("blog_entry"."created" AT TIME ZONE 'UTC') :: date = '2022-07-30' ) ORDER BY "blog_entry"."created" DESC There are plenty of more sophisticated ways to build abstractions over queries in Django - using custom QuerySet methods for example: https://docs.djangoproject.com/en/4.1/topics/db/managers/#calling-custom-queryset-methods-from-the-manager https://docs.djangoproject.com/en/4.1/topics/db/managers/#ca...
- quantified 4y agoCool, thank you for taking the effort to illuminate.