如何使用字典而不是方法参数对 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...
附带问题,有没有比我上面做的更好的方法来设置字典值?例如三元,但如果它不存在,它会使值不存在?
sort_params['sort'] = sort if not None else ''