开发者

Specifying Django Query Filters at Run Time

How do I specify an arbitrary Django query filter at runtime?

Normally one uses filters like so...

query_set = MyModel.objects.filter(name__iexact='foobar')

But what if I have the query filter specifier contained in a string?

query_specifier = "name_iexact='f开发者_运维技巧oobar'"
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)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜