如何使用字符串作为关键字参数?

Iss*_*lly 43 python django

具体来说,我正在尝试使用字符串来任意过滤ORM.我已经尝试过exec和eval解决方案,但我遇到了墙壁.下面的代码不起作用,但这是我知道如何解释我要去的地方的最佳方式

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.
Run Code Online (Sandbox Code Playgroud)

Ign*_*ams 83

d = Image.objects.filter(**{'image__endswith': "jpg"})
Run Code Online (Sandbox Code Playgroud)