from django.db import connection
cursor = connection.cursor()
cursor.execute("SELECT foo FROM bar WHERE baz = %s", [self.baz])
row = cursor.fetchone()
return row
如果你的SQL语句改变了数据库中的数据 -- 比如你使用了 DELETE 或 UPDATE 语句. 你需要调用 connection.commit() 来使你的修改生效.
from django.db import connection
cursor = connection.cursor()
cursor.execute("DELETE FROM bar WHERE baz = %s", [self.baz])
connection.commit()