22import time
33from importlib import import_module
44
5+ import django
56from django .conf import settings
67from django .contrib .sessions .backends .base import UpdateError
78from django .core .exceptions import SuspiciousOperation
@@ -167,9 +168,7 @@ def __init__(self, scope, send):
167168
168169 async def resolve_session (self ):
169170 session_key = self .scope ["cookies" ].get (self .cookie_name )
170- self .scope ["session" ]._wrapped = await database_sync_to_async (
171- self .session_store
172- )(session_key )
171+ self .scope ["session" ]._wrapped = self .session_store (session_key )
173172
174173 async def send (self , message ):
175174 """
@@ -187,7 +186,7 @@ async def send(self, message):
187186 and message .get ("status" , 200 ) != 500
188187 and (modified or settings .SESSION_SAVE_EVERY_REQUEST )
189188 ):
190- await database_sync_to_async ( self .save_session ) ()
189+ await self .save_session ()
191190 # If this is a message type that can transport cookies back to the
192191 # client, then do so.
193192 if message ["type" ] in self .cookie_response_message_types :
@@ -225,12 +224,15 @@ async def send(self, message):
225224 # Pass up the send
226225 return await self .real_send (message )
227226
228- def save_session (self ):
227+ async def save_session (self ):
229228 """
230229 Saves the current session.
231230 """
232231 try :
233- self .scope ["session" ].save ()
232+ if django .VERSION >= (5 , 1 ):
233+ await self .scope ["session" ].asave ()
234+ else :
235+ database_sync_to_async (self .scope ["session" ].save )()
234236 except UpdateError :
235237 raise SuspiciousOperation (
236238 "The request's session was deleted before the "
0 commit comments