Skip to content

Commit 784f58a

Browse files
committed
Fix USE_TZ RemovedInDjango50Warning
Fix this warning, which appears in both the docs build and Django 4.0 tests: ``` /.../python3.8/site-packages/django/conf/__init__.py:199: RemovedInDjango50Warning: The default value of USE_TZ will change from False to True in Django 5.0. Set USE_TZ to False in your project settings if you want to keep the current default behavior. ```
1 parent 5ad50f7 commit 784f58a

File tree

4 files changed

+36
-17
lines changed

4 files changed

+36
-17
lines changed

example/settings.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@
5757
}
5858
]
5959

60+
USE_TZ = True
61+
6062
WSGI_APPLICATION = "example.wsgi.application"
6163

6264

tests/panels/test_sql.py

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -121,26 +121,41 @@ def test_param_conversion(self):
121121
.filter(group_count__lt=10)
122122
.filter(group_count__gt=1)
123123
)
124-
list(User.objects.filter(date_joined=datetime.datetime(2017, 12, 22, 16, 7, 1)))
124+
list(
125+
User.objects.filter(
126+
date_joined=datetime.datetime(
127+
2017, 12, 22, 16, 7, 1, tzinfo=datetime.timezone.utc
128+
)
129+
)
130+
)
125131

126132
response = self.panel.process_request(self.request)
127133
self.panel.generate_stats(self.request, response)
128134

129135
# ensure query was logged
130136
self.assertEqual(len(self.panel._queries), 3)
131137

132-
# Django 4.1 started passing true/false back for boolean
133-
# comparisons in MySQL.
134-
if not (django.VERSION >= (4, 1) and connection.vendor == "mysql"):
135-
self.assertEqual(
136-
tuple(q[1]["params"] for q in self.panel._queries),
137-
('["Foo"]', "[10, 1]", '["2017-12-22 16:07:01"]'),
138-
)
138+
if connection.vendor == "mysql" and django.VERSION < (4, 1):
139+
# Django 4.1 started passing true/false back for boolean
140+
# comparisons in MySQL.
141+
expected_bools = '["Foo", true, false]'
139142
else:
140-
self.assertEqual(
141-
tuple(q[1]["params"] for q in self.panel._queries),
142-
('["Foo", true, false]', "[10, 1]", '["2017-12-22 16:07:01"]'),
143-
)
143+
expected_bools = '["Foo"]'
144+
145+
if connection.vendor == "postgresql":
146+
# PostgreSQL always includes timezone
147+
expected_datetime = '["2017-12-22 16:07:01+00:00"]'
148+
else:
149+
expected_datetime = '["2017-12-22 16:07:01"]'
150+
151+
self.assertEqual(
152+
tuple(q[1]["params"] for q in self.panel._queries),
153+
(
154+
expected_bools,
155+
"[10, 1]",
156+
expected_datetime,
157+
),
158+
)
144159

145160
@unittest.skipUnless(
146161
connection.vendor == "postgresql", "Test valid only on PostgreSQL"

tests/settings.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@
6969
},
7070
]
7171

72+
USE_TZ = True
73+
7274
STATIC_ROOT = os.path.join(BASE_DIR, "tests", "static")
7375

7476
STATIC_URL = "/static/"

tests/test_forms.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
from datetime import datetime
1+
from datetime import datetime, timezone
22

33
from django import forms
44
from django.test import TestCase
55

66
from debug_toolbar.forms import SignedDataForm
77

8-
SIGNATURE = "v02QBcJplEET6QXHNWejnRcmSENWlw6_RjxLTR7QG9g"
8+
SIGNATURE = "-WiogJKyy4E8Om00CrFSy0T6XHObwBa6Zb46u-vmeYE"
99

10-
DATA = {"value": "foo", "date": datetime(2020, 1, 1)}
11-
SIGNED_DATA = f'{{"date": "2020-01-01 00:00:00", "value": "foo"}}:{SIGNATURE}'
10+
DATA = {"value": "foo", "date": datetime(2020, 1, 1, tzinfo=timezone.utc)}
11+
SIGNED_DATA = f'{{"date": "2020-01-01 00:00:00+00:00", "value": "foo"}}:{SIGNATURE}'
1212

1313

1414
class FooForm(forms.Form):
@@ -32,7 +32,7 @@ def test_verified_data(self):
3232
form.verified_data(),
3333
{
3434
"value": "foo",
35-
"date": "2020-01-01 00:00:00",
35+
"date": "2020-01-01 00:00:00+00:00",
3636
},
3737
)
3838
# Take it back to the foo form to validate the datetime is serialized

0 commit comments

Comments
 (0)