def SQL(*keys): def _SQL(func): def _sqlfunc(cls, *args): from django.db import connection cursor = connection.cursor() cursor.execute(func.__doc__, args) rv = cursor.fetchall() if keys: rv = [dict(zip(keys, i)) for i in rv] return rv return classmethod(_sqlfunc) return _SQL #EXAMPLE @SQL('foo', 'bar', 'baz') def foo(cls): '''SELECT foo, AVG(bar) AS bar, AVG(baz) AS baz FROM foos WHERE quuux=%s GROUP BY foo ORDER BY foo''' #INVOCATION Foo.foo(1)