我这里有4个变量需要检查它是真是假。所有的组合将在一起。。4真,3真1假,2真2假,1真3假和全部假。。总的来说,这将产生10++的条件(有点懒于计数)
if game and owner and platform and is_new and for_sale:
game_copy = GameCopy.objects.all()
elif game:
game_copy = GameCopy.objects.filter(game=game)
elif platform:
game_copy = GameCopy.objects.filter(platform=platform)
elif owner:
game_copy = GameCopy.objects.filter(user=owner)
elif is_new:
game_copy = GameCopy.objects.filter(is_new=is_new)
elif for_sale:
game_copy = GameCopy.objects.filter(for_sale=for_sale)
else:
game_copy = GameCopy.objects.all()
return game_copy
我正在寻找一种更精简的方法来解决这种情况。。希望以5到10行结束
您可以将变量更改为一组标志(以唯一整数设置的位)
您可以将所有真实的内容添加到字典中,然后通过unpacking keyword arguments将它们传递给函数
相关问题 更多 >
编程相关推荐