我需要基于变量过滤查询,但前提是此var不为空...这是我尝试过的操作:
rows = Attendance.objects.filter(type= the_type if the_type != "" else '*')
问题出在else '*'
我尝试删除else的部分中,但是它无法工作,因为如果在中使用if之后也需要使用else ternary operator
,也尝试过,else pass
但这给了我一个错误。
我可以if
在queryset之外使用,但是I have more than one dynamic variable that I will use in the same query, so wrapping it inside an if statement won't work well
。
如何使用过滤器参数创建字典:
filter_params = {}
if the_type != "":
filter_params['type'] = the_type
rows = Attendance.objects.filter(**filter_params)
?
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句