Search code examples
djangodjango-querysetdjango-orm

Does Django natively support common table expressions?


To clarify my question I would like to know if it is possible to idiomatically use the Django ORM whilst accessing CTE features. I imagine I could use CTE by writing raw SQL statements but the ability to use the ORM 'syntactic sugar' to bypass hand coding SQL statements was one of the original appeals of Django.


Solution

  • Django doesn't support CTEs directly as these are not common to all databases (MySQL pre-8.0 didn't support it). There are packages that extend the capability of Django's ORM to support CTEs. One of these is django-cte. Note that it only supports PostgreSQL.