如何使用字典而不是方法参数在Django模型上进行过滤? 这就是我现在所拥有的:
class StoreView(TemplateView):
def get(self, request):
# A bunch of gets
sort = request.GET.get('sort')
sort_price = request.GET.get('sort_price')
sort_mfr_method = request.GET.get('mfr_method')
# The params tpsort by
sort_params = {}
if sort is not None:
sort_params['sort'] = sort
if sort_price is not None:
sort_params['sort_price'] = sort_price
if sort_mfr_method is not None:
sort_params['sort_mfr_method'] = sort_mfr_method
# The Model Query
design_list = models.Design.objects.filter(sort_params)
# etc...
顺带问一下,有比我上面做的更好的设置字典值的方法吗?比如三元表达式,但可以使值不存在如果它是None?
sort_params['sort'] = sort if not None else ''