如何使用字符串作为关键字参数? [英] How do I use a string as a keyword argument?
本文介绍了如何使用字符串作为关键字参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
from gblocks.models import Image
f ='image__endswith =jpg'#将在另一个区域脚本化,但以文本< user input>
d = Image.objects.filter(f)
#对于非django pythonistas:
d = Image.objects.filter(image__endswith =jpg)
#将是非动态的等价物。
解决方案
d = Image.objects.filter(** {'image__endswith':jpg})
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.
解决方案
d = Image.objects.filter(**{'image__endswith': "jpg"})
这篇关于如何使用字符串作为关键字参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文