Bug #73940 Django connector empty datetime conversion error
Submitted: 16 Sep 2014 20:51 Modified: 14 Oct 2014 21:13
Reporter: Alexander Allakhverdiyev Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / Python Severity:S2 (Serious)
Version:1.2.3 OS:Any
Assigned to: Peeyush Gupta CPU Architecture:Any
Tags: datetime, Django, null

[16 Sep 2014 20:51] Alexander Allakhverdiyev
Description:
When you try to convert to python empty date "0000-00-00 00:00:00", connector throws AttributeError

mysql/connector/django/base.py:

    def _DATETIME_to_python(self, value, dsc=None):
        """Connector/Python always returns naive datetime.datetime

        Connector/Python always returns naive timestamps since MySQL has
        no time zone support. Since Django needs non-naive, we need to add
        the UTC time zone.

        Returns datetime.datetime()
        """
        if not value:
            return None
        dt = MySQLConverter._DATETIME_to_python(self, value)
        if settings.USE_TZ and timezone.is_naive(dt):  #### dt is None this will throw AttributeError
            dt = dt.replace(tzinfo=timezone.utc)
        return dt

How to repeat:
Convert to python datetime with value "0000-00-00 00:00:00" using Django connector

Suggested fix:
Check dt variable before trying to change tzinfo:

    def _DATETIME_to_python(self, value, dsc=None):
        """Connector/Python always returns naive datetime.datetime

        Connector/Python always returns naive timestamps since MySQL has
        no time zone support. Since Django needs non-naive, we need to add
        the UTC time zone.

        Returns datetime.datetime()
        """
        if not value:
            return None
        dt = MySQLConverter._DATETIME_to_python(self, value)
        if dt is None:  ### addeded check
            return None
        if settings.USE_TZ and timezone.is_naive(dt):
            dt = dt.replace(tzinfo=timezone.utc)
        return dt
[23 Sep 2014 9:43] Geert Vanderkelen
Verified in v1.2, and also v2.0. Thanks for reporting this.

Better way is to fix the Connector catching the None value when converting.
[14 Oct 2014 21:13] Paul DuBois
Noted in 2.0.2, 2.1.1 changelogs.

The Django backend raised an exception when converting "0000-00-00
00:00:00" to None.