如何使用字符串作为关键字参数? [英] How do I use a string as a keyword argument?

查看:146
本文介绍了如何使用字符串作为关键字参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

具体来说,我试图使用一个字符串仲裁过滤ORM。我已经尝试过exec和eval解决方案,但是我正在进入墙壁。下面的代码不起作用,但这是我知道如何解释我想去哪里的最好方法。

  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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆