簡體   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

我如何將temp的值分配給customer_info對象的空查詢集,以便可以將temp與過濾器列表結合起來然后返回它。 基本上,我將拆分搜索框文本,然后使用列表中的每個字符串過濾表並將結果初始結果組合在一起。

您可以使用none()獲得一個空的查詢集:

MyModel.objects.none()

一種替代的方法是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