开发者

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)
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜