28

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)
1
  • Don't hate me for this, since initialization code for django was never resolved in a clean way: Have you tried creating a middleware class, including the middleware class in the list, and raising MiddlewareNotUsed in the middleware constructor AFTER running the code you propose here? Apr 11, 2014 at 21:28

4 Answers 4

78

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!

4
17

You can also try with Adding below option in Database []

'OPTIONS': {
        'init_command': "SET sql_mode='STRICT_TRANS_TABLES'",
    },

Its working.

1
  • 3
    I added this, and I still get the warning. Is there any way to tell if it is taking effect properly?
    – dfrankow
    Mar 20, 2019 at 20:40
1

https://docs.djangoproject.com/en/1.11/ref/databases/#mysql-sql-mode

Setting sql_mode

From MySQL 5.7 onwards and on fresh installs of MySQL 5.6, the default value of the sql_mode option contains STRICT_TRANS_TABLES. That option escalates warnings into errors when data are truncated upon insertion, so Django highly recommends activating a strict mode for MySQL to prevent data loss (either STRICT_TRANS_TABLES or STRICT_ALL_TABLES).

If you need to customize the SQL mode, you can set the sql_mode variable like other MySQL options: either in a config file or with the entry 'init_command': "SET sql_mode='STRICT_TRANS_TABLES'" in the OPTIONS part of your database configuration in DATABASES.

2
  • 2
    It is unclear from that text whether sql_mode should be set in settings.py or in my.cnf.
    – EugZol
    Jan 26, 2021 at 0:25
  • It's quite clear in the documentation:"If you need to customize the SQL mode, you can set the sql_mode variable like other MySQL options: either in a config file or with the entry 'init_command': "SET sql_mode='STRICT_TRANS_TABLES'" in the OPTIONS part of your database configuration in DATABASES."
    – JayB
    Jul 3, 2022 at 6:20
0

If you need to union the queryset, you can use python chain inster than union.

from itertools import chain

gobj_list = user.groups.all()

robj_list = [obj.roles.all() for obj in gobj_list]

ret_list = chain(*robj_list)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.