Django Filter Model by Dictionary(Django Filter Model by Dictionary)
如何使用Dictionary而不是方法参数在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 ''How does one do a filter on a Django Model using a Dictionary rather than method arguments? This is what I have right here:
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...Side Question, is there a better way set the dictionary values than what I'm doing above? Such as a ternary, yet in a way that would make the value not exist if it's none?
sort_params['sort'] = sort if not None else ''最满意答案
您使用字典来传递像这样的关键字参数:
models.Design.objects.filter(**sort_params)有没有内置的方式来有条件地设置一个字典密钥,但如果你做了很多,你可以编写自己的:
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)You use a dictionary to pass keyword arguments like this:
models.Design.objects.filter(**sort_params)
There's no built-in way to conditionally set a 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)更多推荐
发布评论