When I make a raw SQL query in Django, some queries get _ip.magic into the string, and then string formatting raises exceptions since there are not enough or too many parameters.
Sample code was reduced to the minimum set, but still produces “magics”:
> ids = (1, 4)
> curr = 3
> q = User.objects.raw(u"""
SELECT
1
WHERE
a=%s and b=%s AND a.user_id = %s
""", params=(ids, ids, curr))
> print q.query.sql
... a = _ip.magic("s and b=%s AND a.user_id = %s")
(I don’t mean to run this query, just want to successfully generate a SQL.)
Why is _ip.magic there? Depending on the queries, sometimes it wraps a single parameter, sometimes several of them. How to get rid of it?
edit: the solution was to turn off automagic:
>>> _ip.options['automagic'] = 0
_ip.magicis as far as I can tell an IPython function and has nothing to do with django itself.Try running this code in the plain vanilla django shell.