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.
You can also use a CTE in a CREATE VIEW statement, as part of the view's SELECT query. In addition, as of SQL Server 2008, you can add a CTE to the new MERGE statement. This article covers SQL Server common tables expression (CTE) basics. SQL Server supports two types of CTEs-recursive and nonrecursive.
CTEs, like database views and derived tables, enable users to more easily write and maintain complex queries via increased readability and simplification. This reduction in complexity is achieved by deconstructing ordinarily complex queries into simple blocks to be used, and reused if necessary, in rewriting the query.
CTE was introduced in SQL Server 2005, the common table expression (CTE) is a temporary named result set that you can reference within a SELECT, INSERT, UPDATE, or DELETE statement. You can also use a CTE in a CREATE a view, as part of the view's SELECT query.
A QuerySet is a collection of data from a database. A QuerySet is built up as a list of objects. QuerySets makes it easier to get the data you actually need, by allowing you to filter and order the data.
Django doesn't support CTEs directly as these are not common to all databases (MySQL doesn't support it). There are packages that extend the capability of Django's ORM to support CTEs. One of these is django-cte-trees. Note that it only supports PostgreSQL.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With