How passing string on filter keyword to Django Objects Model?

喜你入骨 提交于 2019-11-30 14:16:02
Sam Starling

You can do something like this:

my_filter = {}
my_filter[my_keyword] = my_filter_value

my_object = MyModel.objects.filter(**my_filter)

As an example, your variables might be:

my_keyword = 'price__gte'
my_filter_value = 10

Which would result in getting all objects with a price >= 10. And if you want to query on more than one field, you can just add another line below my_filter[my_keyword]:

my_filter[my_keyword] = my_filter_value
my_filter[my_other_keyword] = my_other_filter_value
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!