Django Filter Model by Dictionary(Django Filter Model by Dictionary)

系统教程 行业动态 更新时间:2024-06-14 17:03:54
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)

更多推荐

本文发布于:2023-04-24 14:05:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/986654c846bf9bf0247096b1cc1852a6.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Filter   Django   Dictionary   Model

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!