如何在运行时指定任意Django查询过滤器?
通常使用像这样的过滤器......
query_set = MyModel.objects.filter(name__iexact='foobar')
但是如果我在字符串中包含查询过滤器说明符怎么办?
query_specifier = "name_iexact='foobar'"
query_set = MyModel.objects.filter(query_specifier) # <-- This doesn't work; How can I do this?
答案 0 :(得分:9)
query_specifier = {
'name__iexact': 'foobar'
}
query_set = MyModel.objects.filter(**query_specifier)