在django中强制严格的sql模式

时间:2022-09-04 11:04:42

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.

我正在尝试强制我的django项目始终使用严格的sql_mode。除了在manage.py中添加以下内容之外,还有其他方法吗?看起来过于复杂。

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)

2 个解决方案

#1


32  

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设置中提供的自定义数据库OPTIONS如下:

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

Hope it helps anyone!

希望它能帮到任何人!

#2


7  

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

您也可以尝试使用Database []中的添加以下选项

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

Its working.

它的工作。

#1


32  

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设置中提供的自定义数据库OPTIONS如下:

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

Hope it helps anyone!

希望它能帮到任何人!

#2


7  

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

您也可以尝试使用Database []中的添加以下选项

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

Its working.

它的工作。