How do I use a string as a keyword argument?

Posted by Issac Kelly on Stack Overflow See other posts from Stack Overflow or by Issac Kelly
Published on 2010-05-28T21:26:41Z Indexed on 2010/05/28 21:32 UTC
Read the original article Hit count: 249

Filed under:
|

Specifically, I'm trying to use a string to arbitrairly filter the ORM. I've tried exec and eval solutions, but I'm running into walls. The code below doesn't work, but it's the best way I know how to explain where I'm trying to go

from gblocks.models import Image
f = 'image__endswith="jpg"' # Would be scripted in another area, but passed as text <user input>
d = Image.objects.filter(f)


#for the non-django pythonistas:
d = Image.objects.filter(image__endswith="jpg")
# would be the non-dynamic equivalent.

© Stack Overflow or respective owner

Related posts about python

Related posts about django