开发者

Specifying Django Query Filters at Run Time

开发者 https://www.devze.com 2023-03-02 23:10 出处:网络
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\')

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

精彩评论

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