Django 中的控制查询集(过滤器,对象 Q)?

发布于 2024-08-16 07:54:44 字数 1671 浏览 9 评论 0原文

基于 URL,

querydict = {customer_type:val1,tag:[], city:[],last_contact:valdate}

show/?customer_type=All&tag=2,3&city=3&last_contact=29/12/2009

我将按方法进行过滤:

def get_filter_result(customer_type, tag_selected, city_selected, last_contact_filled):
    if customer_type=has value:
         I filter by this 
       #queryset = Customer.objects.filter(Q(type__name=customer_type))
    if tag = has value :
         I filter by this
         #queryset = Customer.objects.filter(Q(type__name=customer_type)
                                              Q(type__name=tag))
    if city = has value:
        I filter by this
        #queryset = Customer.objects.filter(Q(type__name=customer_type)
                                              Q(type__name=tag),
                                              Q(type__name=city))
    if last_contact = has value:
        I filter by this
        #queryset = Customer.objects.filter(Q(type__name=customer_type)
                                              Q(type__name=tag),
                                              Q(type__name=city),
                                              Q(type__name=last_contact))

有人可以帮助提供一个想法来实现我的方法比这更简单和灵活吗? 如果它们的值缺失或等于 None(不传递任何值) 所以 if...else...条件将控制很多时间并且代码会更大..

for example :
      show/?customer_type=All&tag=&city=&last_contact=
      show/?customer_type=All&tag=2,3&city=3&last_contact=29/12/2009
      show/?customer_type=&tag=2,3&city=3&last_contact=
      show/?customer_type=All&tag=2,3&city=&last_contact=29/12/2009

谢谢

Base On URL

querydict = {customer_type:val1,tag:[], city:[],last_contact:valdate}

show/?customer_type=All&tag=2,3&city=3&last_contact=29/12/2009

I am going to filter by made the method:

def get_filter_result(customer_type, tag_selected, city_selected, last_contact_filled):
    if customer_type=has value:
         I filter by this 
       #queryset = Customer.objects.filter(Q(type__name=customer_type))
    if tag = has value :
         I filter by this
         #queryset = Customer.objects.filter(Q(type__name=customer_type)
                                              Q(type__name=tag))
    if city = has value:
        I filter by this
        #queryset = Customer.objects.filter(Q(type__name=customer_type)
                                              Q(type__name=tag),
                                              Q(type__name=city))
    if last_contact = has value:
        I filter by this
        #queryset = Customer.objects.filter(Q(type__name=customer_type)
                                              Q(type__name=tag),
                                              Q(type__name=city),
                                              Q(type__name=last_contact))

Anybody Help give an idea to implement my method more simple and flexible than this?
if value of them are missing or equal None(No value is passed)
so if... else.... condition will control alots of time and code will be larger..

for example :
      show/?customer_type=All&tag=&city=&last_contact=
      show/?customer_type=All&tag=2,3&city=3&last_contact=29/12/2009
      show/?customer_type=&tag=2,3&city=3&last_contact=
      show/?customer_type=All&tag=2,3&city=&last_contact=29/12/2009

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

老子叫无熙 2024-08-23 07:54:44
def get_filter_result(customer_type=None, tag_selected=None, city_selected=None, last_contact_filled=None):
    qdict = {}
    if customer_type is not None:
        qdict['type__name'] = customer_type
    if tag is not None:
        <repeat as appropriate>
    queryset = Customer.objects.filter(**qdict)
def get_filter_result(customer_type=None, tag_selected=None, city_selected=None, last_contact_filled=None):
    qdict = {}
    if customer_type is not None:
        qdict['type__name'] = customer_type
    if tag is not None:
        <repeat as appropriate>
    queryset = Customer.objects.filter(**qdict)
腹黑女流氓 2024-08-23 07:54:44

如果您想对所有查询进行 AND 操作(如示例中所示),您可以创建参数字典,然后将此字典作为参数调用 filter 方法:

def get_filter_result(**kwargs):
    params = {}
    #delete items with empty strings
    for key in kwargs:
        if kwargs[key]:
            params[key] = kwargs[key]

    queryset = Customer.objects.filter(**params)

If you want to AND all your queries (like in your example), you can create dictionary of parameters, and then call filter method with this dictionary as an argument:

def get_filter_result(**kwargs):
    params = {}
    #delete items with empty strings
    for key in kwargs:
        if kwargs[key]:
            params[key] = kwargs[key]

    queryset = Customer.objects.filter(**params)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文