global name 'connection' is not definedというエラーが出る場合の対処方法
データベースに接続するときに、カスタマイズしたSQL等を発行するときに、
以下のようなエラーが出る場合があります。
global name 'connection' is not defined Django Version: 1.2.4 Exception Type: NameError Exception Value: global name 'connection' is not defined
これはconnectionをdjango.dbから呼び出せばOKです。
from django.db import connection
を使用する関数で宣言しましょう。
def my_db_sql(self): from django.db import connection cursor = connection.cursor() cursor.execute("SELECT huga FROM hoge WHERE name = %s",[self.word]) row = cursor.fetchone() return row
以上です。