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)
精彩评论