Skip to content

Commit a57f885

Browse files
committed
BUG19667984: Fix converting invalid datetime values in Django backend
Connector/Python Django backend was raising error while converting an invalid datetime value instead of returning None. We fix this issue by adding a check for invalid values. Unit tests are updated.
1 parent 4b2e59a commit a57f885

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

lib/mysql/connector/django/base.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ def _DATETIME_to_python(self, value, dsc=None):
9292
if not value:
9393
return None
9494
dt = MySQLConverter._DATETIME_to_python(self, value)
95+
if dt is None:
96+
return None
9597
if settings.USE_TZ and timezone.is_naive(dt):
9698
dt = dt.replace(tzinfo=timezone.utc)
9799
return dt

tests/test_django.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,3 +267,16 @@ def test__TIME_to_python(self):
267267
django_converter = DjangoMySQLConverter()
268268
self.assertEqual(datetime.time(10, 11, 12),
269269
django_converter._TIME_to_python(value, dsc=None))
270+
271+
def test__DATETIME_to_python(self):
272+
value = b'1990-11-12 00:00:00'
273+
django_converter = DjangoMySQLConverter()
274+
self.assertEqual(datetime.datetime(1990, 11, 12, 0, 0, 0),
275+
django_converter._DATETIME_to_python(value, dsc=None))
276+
277+
settings.USE_TZ = True
278+
value = b'0000-00-00 00:00:00'
279+
django_converter = DjangoMySQLConverter()
280+
self.assertEqual(None,
281+
django_converter._DATETIME_to_python(value, dsc=None))
282+
settings.USE_TZ = False

0 commit comments

Comments
 (0)