9.6 Connector/Python Django Back End (original) (raw)

Connector/Python includes a mysql.connector.django module that provides a Django back end for MySQL. This back end supports new features found as of MySQL 5.6 such as fractional seconds support for temporal data types.

Django Configuration

Django uses a configuration file namedsettings.py that contains a variable calledDATABASES (seehttps://docs.djangoproject.com/en/1.5/ref/settings/#std:setting-DATABASES). To configure Django to use Connector/Python as the MySQL back end, the example found in the Django manual can be used as a basis:

DATABASES = {
    'default': {
        'NAME': 'user_data',
        'ENGINE': 'mysql.connector.django',
        'HOST': '127.0.0.1',
        'PORT': 3306,
        'USER': 'mysql_user',
        'PASSWORD': 'password',
        'OPTIONS': {
          'autocommit': True,
          'use_oure': True,
          'init_command': "SET foo='bar';"
        },
    }
}

It is possible to add more connection arguments usingOPTIONS.

Support for MySQL Features

Django can launch the MySQL client applicationmysql. When the Connector/Python back end does this, it arranges for the sql_mode system variable to be set to TRADITIONAL at startup.

Some MySQL features are enabled depending on the server version. For example, support for fractional seconds precision is enabled when connecting to a server from MySQL 5.6.4 or higher. Django'sDateTimeField is stored in a MySQL column defined as DATETIME(6), andTimeField is stored asTIME(6). For more information about fractional seconds support, see Fractional Seconds in Time Values.

Using a custom class for data type conversation is supported as a subclass of_mysql.connector.django.base.DjangoMySQLConverter_. This support was added in Connector/Python 8.0.29.