jonny jonny - 2 months ago 32
MySQL Question

Force strict sql mode in django

I'm trying to force my django project to always use strict

sql_mode
. Is there another way than putting the following in
manage.py
? It seems overly complicated.

def set_strict_sql_mode(sender, **kwargs):
from django.conf import settings
if settings.DATABASES['default']['ENGINE'] == 'django.db.backends.mysql':
from django.db import connection
cursor = connection.cursor()
cursor.execute('SET session sql_mode=traditional')

from django.core.signals import request_started
request_started.connect(set_strict_sql_mode)

Answer

Actually asking proved to be a good rubber duck. Just after asking, I found the custom database OPTIONS one can supply in the DATABASES settings like this:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'OPTIONS': {
            'sql_mode': 'traditional',
        }
    }
}

Hope it helps anyone!