Skip to content

Instantly share code, notes, and snippets.

@theY4Kman
Created March 11, 2014 16:49
Show Gist options
  • Save theY4Kman/9489898 to your computer and use it in GitHub Desktop.
Save theY4Kman/9489898 to your computer and use it in GitHub Desktop.
Disables MySQL warnings in Django. I place it somewhere in my settings module. My use case is I develop on multiple branches, some with different database migrations, and I don't have a proper setup with a different database for each, so when I have to add a hotfix, MySQL sometimes complains about stupid shit. I don't really care about it, so I …
# Ignoring MySQL warnings, such as:
# "Field 'discount_id' doesn't have a default value"
IGNORE_MYSQL_WARNINGS = True
if IGNORE_MYSQL_WARNINGS:
_GOT_FIRST_REQUEST = False
def on_first_request(signal, sender):
global _GOT_FIRST_REQUEST
if not _GOT_FIRST_REQUEST:
_GOT_FIRST_REQUEST = True
from django.db.backends.mysql.base import CursorWrapper
import MySQLdb as Database
def _ignore_warnings(fn):
def _execute(*args, **kwargs):
try:
return fn(*args, **kwargs)
except Database.Warning as e:
return None
return _execute
CursorWrapper.execute = _ignore_warnings(CursorWrapper.execute)
CursorWrapper.executemany = _ignore_warnings(CursorWrapper.executemany)
from django.core.signals import request_started
request_started.connect(on_first_request)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment