Skip to content

Commit 7d582d9

Browse files
committed
Work around psycopg2.9 + django 3.0 compatibility issue
1 parent cf87e7a commit 7d582d9

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

settings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
'default': dj_database_url.config(default='postgres:///psqlextra'),
1212
}
1313

14-
DATABASES['default']['ENGINE'] = 'psqlextra.backend'
14+
DATABASES['default']['ENGINE'] = 'tests.psqlextra_test_backend'
1515

1616
LANGUAGE_CODE = 'en'
1717
LANGUAGES = (

tests/psqlextra_test_backend/__init__.py

Whitespace-only changes.

tests/psqlextra_test_backend/base.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from datetime import timezone
2+
3+
import django
4+
5+
from django.conf import settings
6+
7+
from psqlextra.backend.base import DatabaseWrapper as PSQLExtraDatabaseWrapper
8+
9+
10+
class DatabaseWrapper(PSQLExtraDatabaseWrapper):
11+
# Works around the compatibility issue of Django <3.0 and psycopg2.9
12+
# in combination with USE_TZ
13+
#
14+
# See: https://github.com/psycopg/psycopg2/issues/1293#issuecomment-862835147
15+
if django.VERSION < (3, 1):
16+
17+
def create_cursor(self, name=None):
18+
cursor = super().create_cursor(name)
19+
cursor.tzinfo_factory = (
20+
lambda offset: timezone.utc if settings.USE_TZ else None
21+
)
22+
23+
return cursor

0 commit comments

Comments
 (0)