@@ -82,30 +82,40 @@ def generate_sql(cls, connection, query, as_sql):
82
82
ctes .append (template .format (name = qn (cte .name ), query = cte_sql ))
83
83
params .extend (cte_params )
84
84
85
- explain_query = getattr (query , "explain_query" , None )
86
- sql = []
87
- if explain_query :
85
+ # Required due to breaking change in django commit
86
+ # fc91ea1e50e5ef207f0f291b3f6c1942b10db7c7
87
+ if django .VERSION >= (4 , 0 ):
88
+ explain_attribute = "explain_info"
89
+ explain_info = getattr (query , explain_attribute , None )
90
+ explain_format = getattr (explain_info , "format" , None )
91
+ explain_options = getattr (explain_info , "options" , {})
92
+ else :
93
+ explain_attribute = "explain_query"
88
94
explain_format = getattr (query , "explain_format" , None )
89
95
explain_options = getattr (query , "explain_options" , {})
96
+
97
+ explain_query_or_info = getattr (query , explain_attribute , None )
98
+ sql = []
99
+ if explain_query_or_info :
90
100
sql .append (
91
101
connection .ops .explain_query_prefix (
92
102
explain_format ,
93
103
** explain_options
94
104
)
95
105
)
96
- # this needs to get set to False so that the base as_sql() doesn't
106
+ # this needs to get set to None so that the base as_sql() doesn't
97
107
# insert the EXPLAIN statement where it would end up between the
98
108
# WITH ... clause and the final SELECT
99
- query . explain_query = False
109
+ setattr ( query , explain_attribute , None )
100
110
101
111
if ctes :
102
112
# Always use WITH RECURSIVE
103
113
# https://www.postgresql.org/message-id/13122.1339829536%40sss.pgh.pa.us
104
114
sql .extend (["WITH RECURSIVE" , ", " .join (ctes )])
105
115
base_sql , base_params = as_sql ()
106
116
107
- if explain_query :
108
- query . explain_query = explain_query
117
+ if explain_query_or_info :
118
+ setattr ( query , explain_attribute , explain_query_or_info )
109
119
110
120
sql .append (base_sql )
111
121
params .extend (base_params )
0 commit comments