You use a dictionary to pass keyword arguments as follows:
models.Design.objects.filter(**sort_params)
There is no built-in way to conditionally set the dict key, but if you do this a lot, you can write your own:
def set_if_not_none(mapping, key, value): if value is not None: mapping[key] = value 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 = {} set_if_not_none(sort_params, 'sort', sort) set_if_not_none(sort_params, 'sort_price', sort_price) set_if_not_none(sort_params, 'sort_mfr_method', sort_mfr_method) # The Model Query design_list = models.Design.objects.filter(**sort_params)
Pavel anossov
source share