我想要使用一个字符串来任意过滤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.
ast.literal_eval()
。 - Ignacio Vazquez-Abramsdatetime.date(2005,1,3)
作为有效的筛选器,那么我能否使用常规的eval()函数? - Issac Kelly