如何在运行时指定任意 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?
query_specifier = {
'name__iexact': 'foobar'
}
query_set = MyModel.objects.filter(**query_specifier)