1
私はdjangoの新機能で、if文のほかに条件付きでフィルタをかけるより効率的な方法があるのだろうかと思っていました。ローカル変数に基づくDjango条件付きフィルタ
は考える:
test_names = ["all"]
test_types = ["a", "b", "c"]
... (more lists)
を私はこれを行うことができます知っている:
q = tests.objects.all()
if test_names[0] == "all":
q = q.all()
else:
q = q.filter("name__in=test_names")
if test_types[0] == "all":
q = q.all()
else:
q = q.filter("type__in=test_type")
etc...
私はこのような何か希望:私は私ので、if文を避けたい
q = test.objects \
.filter((if test_names[0]=="all") "name__in=test_names") \
.filter((if test_types[0]=="all") "type__in=test_types") \
...etc
を"test_names"のような異なるリストに基づいて同じクエリデータに対してこれを何回も実行しなければなりません。