1+ import datetime
12import logging
23from functools import partial
34from importlib import import_module
1011)
1112from django .db .backends .postgresql .creation import DatabaseCreation as Psycopg2DatabaseCreation
1213from django .dispatch import Signal
14+ from django .utils import version
1315try :
1416 from django .utils .asyncio import async_unsafe
1517except ImportError :
1618 # dummy decorator
1719 def async_unsafe (func ):
1820 return func
1921try :
22+ # django 2.2
2023 from django .db .backends .postgresql .utils import utc_tzinfo_factory
24+ from django .utils .timezone import utc
25+
26+ def utc_tzinfo_factory (offset ):
27+ zero = 0
28+ # psycopg>=2.9 sends offset as timedelta
29+ if isinstance (offset , datetime .timedelta ):
30+ zero = datetime .timedelta ()
31+ if offset != zero :
32+ raise AssertionError ("database connection isn't set to UTC" )
33+ return utc
34+
2135except ImportError :
2236 utc_tzinfo_factory = None
2337from sqlalchemy import event
@@ -40,10 +54,11 @@ def async_unsafe(func):
4054pool_args ['poolclass' ] = pool_cls
4155
4256db_pool = manage (Database , ** pool_args )
43- pool_disposed = Signal (providing_args = [ "connection" ] )
57+ pool_disposed = Signal ()
4458
4559log = logging .getLogger ('z.pool' )
4660
61+ django_version = version .get_version_tuple (version .get_version ())
4762
4863def _log (message , * args ):
4964 log .debug (message )
@@ -101,12 +116,13 @@ def create_cursor(self, name=None):
101116 name , scrollable = False , withhold = self .connection .autocommit )
102117 else :
103118 cursor = self ._pool_connection .cursor ()
104- cursor .tzinfo_factory = utc_tzinfo_factory if settings .USE_TZ else None
119+ cursor .tzinfo_factory = self . tzinfo_factory if settings .USE_TZ else None
105120 return cursor
106121
107122 def tzinfo_factory (self , offset ):
108- if utc_tzinfo_factory is not None :
109- return utc_tzinfo_factory
123+ if utc_tzinfo_factory :
124+ # for Django 2.2
125+ return utc_tzinfo_factory (offset )
110126 return self .timezone
111127
112128 def dispose (self ):
@@ -141,6 +157,8 @@ def get_new_connection(self, conn_params):
141157 if self .isolation_level != c .isolation_level :
142158 c .set_session (isolation_level = self .isolation_level )
143159
160+ if django_version >= (3 , 1 , 1 ):
161+ psycopg2 .extras .register_default_jsonb (conn_or_curs = c , loads = lambda x : x )
144162 return c
145163
146164 def is_usable (self ):
0 commit comments