<div class="gmail_quote">On Fri, Nov 13, 2009 at 5:10 PM, scoopseven <span dir="ltr"><<a href="mailto:mark.kecko@gmail.com">mark.kecko@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
I actually had a queryset that was dynamically generated, so I ended<br>
up having to use the eval function, like this...<br>
<br>
d = {}<br>
for thing in things:<br>
        query_name = 'thing_' + str(<a href="http://thing.id" target="_blank">thing.id</a>)<br>
        query_string = 'Thing.objects.filter(type=' + str(<a href="http://thing.id" target="_blank">thing.id</a>) +<br>
').order_by(\'-date\')[:3]'<br>
        executable_string = query_name + ' = Thing.objects.filter<br>
(type=' + str(<a href="http://thing.id" target="_blank">thing.id</a>) + ').order_by(\'-date\')[:3]'<br>
        exec(executable_string)<br>
        d[query_name] = eval(query_string)<br>
<br>
And no, this is not based on user-generated input.<br></blockquote></div><br>I don't get it.  If you're already storing the name to query mapping in a dictionary, why do you also need to use exec to do the same thing in the local namespace?  If you're generating these names dynamically (which you obviously are, based on this snippet), it's going to be faster and more legible if you just look them up in the dictionary, instead of having to use exec and eval all over the place.<br>
<br>Then all you'd need is something like:<br><br>d = {}<br>
for thing in things:<br>       query_name = 'thing_%d' % <a href="http://thing.id">thing.id</a><br>       d[query_name] = Thing.objects.filter(type=<a href="http://thing.id">thing.id</a>).order_by('-date')[:3]<br>

<br>then later you just look them up as d['thing_1'] (or d[thing_name] if you're dynamically generating the names again).<br><br>-- <br>Jerry<br>