假如我有个数据查询方法,用户根据不同条件在库里根据不同的字段查询数据:
def test(name){
if name = 'a':
SqlModele.objects.filter(condition1='xxx').first()
elif name = 'b':
SqlModele.objects.filter(condition2='xxx').first()
elif name = 'c':
SqlModele.objects.filter(condition3='xxx').first()
}
请问我如何把上文代码中的查询条件( condition1 、condition2 、condition3 )通过函数传参的形式传入方法呢?想要的效果是:
def test(name){
if name = 'a':
query_condition=condition1
elif name = 'b':
query_condition=condition2
elif name = 'c':
query_condition=condition3
SqlModele.objects.filter(query_condition='xxx').first()
}
1
erikk0 2022-10-14 19:57:38 +08:00
原生 sql 语句 fix everything
|
2
mokiki 2022-10-14 20:03:12 +08:00
关键词 kwargs
举例: d = {"k": 1} def f(k): print(k) f(**d) |
3
zhoudaiyu 2022-10-14 20:06:56 +08:00
@erikk0 #1 <script src="https://gist.github.com/BiTree/cdc9978dc23c1be34c46a008855ed43f.js"></script>
|
4
XiaoxiaoPu 2022-10-14 21:28:43 +08:00
|
5
tristankuo 2022-10-15 19:14:52 +08:00
SqlModele.objects.filter(**{query_condition:'xxx'}).first()
|
6
amlee 2022-10-19 05:37:05 +08:00
condition = {'a': condition1, 'b': condition2, 'c': codition3}
def test(name){ query_condition=condition[name] SqlModele.objects.filter(query_condition=query_condition).first() } |
7
amlee 2022-10-19 05:38:29 +08:00
不是,我回完了才看出来,你这 python 的函数定义怎么有大括号啊。。。。。。。。。。。。
|