I'm trying to patch this bug and am running into problems. django-pyodbc is an SQL Server backend to django, and the bug causes any sql to be converted to a bytes type in python 3.x, which raises a TypeError when it is attempted to be executed (must be string or unicode).
Here is the code I have at my_app/pyodbc_patch/base.py (the only file in that folder):
import django_pyodbc.base
class CursorWrapper(django_pyodbc.base.CursorWrapper):
def format_sql(self, sql, n_params=None):
if n_params is not None:
try:
sql = sql % tuple('?' * n_params)
except:
pass
else:
if '%s' in sql:
sql = sql.replace('%s', '?')
return sql
class DatabaseFeatures(django_pyodbc.base.DatabaseFeatures):
pass
class DatabaseWrapper(django_pyodbc.base.DatabaseWrapper):
pass
My settings.DATABASES
:
DATABASES = {
'default': {
'ENGINE': 'my_app.pyodbc_patch',
'HOST': '...',
'NAME': '...',
'OPTIONS': {
'host_is_server': True
}
}
}
As far as I can tell from the django source, django.db.utils.ConnectionHandler
gets the engine from DATABASES
and lets django.db.utils.load_backend
import its base
module. However, the error I get is exactly the same as if I hadn't applied the patch. It makes completely no sense to me, because pyodbc_patch imports django_pyodbc itself, so I cannot imagine the faulty version of CursorWrapper.format_sql
is being called even earlier.
The other option is to fix my local copy of django_pyodbc. Unfortunately I only have Python33/Lib/site-packages/django_pyodbc-0.2.3-py3.3.egg and I have no idea how to edit egg files. I really am at my wits' end. It's my first foray into python and django, and I would have absolutely stuck to the ruby and rails that I know if I'd known it would be this messy.
Any help greatly appreciated.