简体   繁体   English

获取在Django中设置的空或空查询

[英]Get null or empty query set in django

    def get_queryset(self):
        key = self.request.GET['search_text']
        customer_list = customer_info.objects.all()
        # temp =
        for term in key.split():
            temp = temp | customer_list.filter(Q(fName__icontains=term)|Q(lName__icontains=term))
        return temp

How can I assign the value of temp to null query set of customer_info objects so that I could union temp with the filter list then return it. 我如何将temp的值分配给customer_info对象的空查询集,以便可以将temp与过滤器列表结合起来然后返回它。 Basically, I am splitting the search box text and then filtering the table with each string in the list and combining the result initial result. 基本上,我将拆分搜索框文本,然后使用列表中的每个字符串过滤表并将结果初始结果组合在一起。

You can get an empty queryset with none() : 您可以使用none()获得一个空的查询集:

MyModel.objects.none()

An alternative approach is to or the Q() objects together instead of querysets: 一种替代的方法是orQ()代替对象的查询集在一起:

q = Q()
for term in key.split():
    q = q | Q(fName__icontains=term) | Q(lName__icontains=term)
return customer_info.objects.filter(q)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM