How do I pass in a field to filter a Django model?
I'm have a function that accepts two parameters: filter_field and filter开发者_Python百科_value. I want to filter my model like so:
Alert.objects.filter(filter_field=filter_value)
However, I get the following error:
Cannot resolve keyword 'filter_field' into field. Choices are ...
Is there way to pass in the string that is being stored in filter_field, and not the literal string 'filter_field'?
You can pass keyword arguments to filter:
kwargs = {filter_field: filter_value}
Alert.objects.filter(**kwargs)
精彩评论