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)
Open the my. ini file. *On the line with "sql_mode", modify the value to turn strict mode ON/OFF. Save the file.
Strict SQL Mode. Strict mode controls how MySQL handles invalid or missing values in data-change statements such as INSERT or UPDATE . A value can be invalid for several reasons. For example, it might have the wrong data type for the column, or it might be out of range.
To use MySql as the backend engine for a Django project, we need to follow a simple setup: Install the MySql Server (we can also use a remote one) Install the Mysql Python driver - used by Django to connect and communicate. Create the Mysql database and the user.
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!
You can also try with Adding below option in Database []
'OPTIONS': { 'init_command': "SET sql_mode='STRICT_TRANS_TABLES'", },
Its working.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With