变量可以在查询集的.filter中使用

San*_*dra 0 python django filter

在python/Django中使用过滤器中变量的正确方法是什么?从概念上讲,我想这样做:

whereconditionfield = 'rate_of_interest'
whereoperator = 'gte'
arri = 50.0
myqsfiltered=myqs.objects.filter(whereconditionfield__whereoperator=arri)
Run Code Online (Sandbox Code Playgroud)

这样的事情可能吗?

ale*_*cxe 5

创建一个字典并将其解压缩到关键字参数中:

kwargs = {whereconditionfield + "__" + whereoperator: arri}
myqs.objects.filter(**kwargs)
Run Code Online (Sandbox Code Playgroud)