File tree 2 files changed +24
-0
lines changed
2 files changed +24
-0
lines changed Original file line number Diff line number Diff line change @@ -112,6 +112,15 @@ def queryset(self):
112
112
if cte_query .values_select :
113
113
query .set_values (cte_query .values_select )
114
114
qs ._iterable_class = ValuesIterable
115
+ if django .VERSION >= (5 , 2 ):
116
+ for alias in cte_query .values_select :
117
+ if (
118
+ alias not in cte_query .annotations
119
+ and cte_query .has_select_fields
120
+ and alias in cte_query .selected
121
+ ):
122
+ col = CTEColumnRef (alias , self .name , cte_query .resolve_ref (alias ).output_field )
123
+ query .add_annotation (col , alias )
115
124
if cte_query .annotations :
116
125
for alias , value in cte_query .annotations .items ():
117
126
col = CTEColumnRef (alias , self .name , value .output_field )
Original file line number Diff line number Diff line change @@ -318,3 +318,18 @@ def make_regions_cte(cte):
318
318
self .assertTrue (
319
319
str (query .query ).startswith ('WITH RECURSIVE "cte" AS MATERIALIZED' )
320
320
)
321
+
322
+ def test_recursive_self_queryset (self ):
323
+ def make_regions_cte (cte ):
324
+ return Region .objects .filter (
325
+ pk = "earth"
326
+ ).values ("pk" ).union (
327
+ cte .join (Region , parent = cte .col .pk ).values ("pk" )
328
+ )
329
+ cte = With .recursive (make_regions_cte )
330
+ queryset = cte .queryset ().with_cte (cte ).order_by ("pk" )
331
+ print (queryset .query )
332
+ self .assertEqual (list (queryset ), [
333
+ {'pk' : 'earth' },
334
+ {'pk' : 'moon' },
335
+ ])
You can’t perform that action at this time.
0 commit comments