我有以下看法:
def process(request):
if request.method == 'POST':
data = request.POST
results = Specs.objects.filter(screenGroup = data['screen_user'], storage = data['storage_user'], mSystem = data['system_user'] )
context = {'results' : results}
return render(request, 'process.html', context)
当用户输入三个值时,它会正确过滤,但是当它只输入一个或两个(或什么都不输入)时,它会过滤传递值 None。如果未设置过滤器,有什么方法可以忽略它?
谢谢!
编辑:
下面的代码是有效的,但它显然是一种非常低效的方式:
def process(request):
if request.method == 'POST':
data = request.POST
if(data['screen_user'] != None):
results = Specs.objects.filter(screenGroup = data['screen_user'])
elif (data['storage_user'] != None):
results = Specs.objects.filter(storage = data['storage_user'])
else:
results = Specs.objects.all()
#plus all the other options...
context = {'results' : results}
return render(request, 'process.html', context)