2017-04-26 52 views
0

我知道這是一個認識問題,但聽我說,我目前在query_params在我看來,這樣的篩選:鏈過濾器查詢參數

filter_date = self.request.query_params.get('filter_date', None) 
if filter_date is not None: 
    queryset = queryset.filter(next_action_date__gte=filter_date) 

return queryset 

和我展示我的聯繫人通過next_action_date,現在我有這樣的自定義數據結構,我需要將它添加到這個過濾器,它會顯示從最重要不重要我接觸,我試圖將其添加到過濾器,但我沒有得到任何數據,所以我怎麼能適當地完成這項工作,有人可以告訴我嗎?

這是我get_queryset:

def get_queryset(self): 
    queryset = LeadContact.objects.none() 
    user = self.request.user 
    if user.has_perm('cms_sales.can_view_full_lead_contact_list'): 
     queryset = LeadContact.objects.all() 
    elif user.has_perm('cms_sales.can_view_lead_contact'): 
     queryset = LeadContact.objects.filter(account_handler=user) 

    filter_date = self.request.query_params.get('filter_date', None) 

    # This is the custom ordering data structure 
    virgin_data = LeadContact.objects.filter(status=LeadContactConstants.STATUS_PRISTINE) 
    contacted_data = LeadContact.objects.filter(status=LeadContactConstants.STATUS_CONTACTED) 
    qualified_data = LeadContact.objects.filter(status=LeadContactConstants.STATUS_QUALIFIED) 
    client_data = LeadContact.objects.filter(status=LeadContactConstants.STATUS_CLIENT) 
    # This needs to be added to the filter so it will properly order 
    # contacts 
    order_data = list(client_data) + list(qualified_data) + list(contacted_data) + list(virgin_data) 

    if filter_date is not None: 
     queryset = queryset.filter(next_action_date__gte=filter_date) 

    return queryset 
+0

必須結果來查詢集?它可以排序,正常的對象列表嗎? –

+0

@MateuszKnapczyk好,我需要的鞋我的聯繫人通過'next_action_date'並把它們正確排序,我很開放的建議,你能告訴我,你需要更多的數據? – PetarP

回答

1

如果您需要查詢集結果,嘗試用queryset = queryset.filter(next_action_date__gte=filter_date).order_by('status'),但它可能不會是你想要的順序。

但是如果你僅僅需要一個篩選,排序列表(沒有一個QuerySet),你可以先應用過濾器,然後通過獲取聯繫人的狀態和把它們連在一起。

def get_queryset(self): 
    queryset = LeadContact.objects.none() 
    user = self.request.user 
    if user.has_perm('cms_sales.can_view_full_lead_contact_list'): 
     queryset = LeadContact.objects.all() 
    elif user.has_perm('cms_sales.can_view_lead_contact'): 
     queryset = LeadContact.objects.filter(account_handler=user) 

    filter_date = self.request.query_params.get('filter_date', None) 
    if filter_date is not None: 
     queryset = queryset.filter(next_action_date__gte=filter_date) 

    # Filter our queryset already filtered by date (if given) 
    virgin_data = list(queryset.filter(status=LeadContactConstants.STATUS_PRISTINE)) 
    contacted_data = list(queryset.filter(status=LeadContactConstants.STATUS_CONTACTED)) 
    qualified_data = list(queryset.filter(status=LeadContactConstants.STATUS_QUALIFIED)) 
    client_data = list(queryset.filter(status=LeadContactConstants.STATUS_CLIENT)) 
    # Just add them together 
    order_data = client_data + qualified_data + contacted_data + virgin_data  

    return order_data 

編輯

我已經找到一種方法更好的解決方案here

order = [ 
    LeadContactConstants.STATUS_CLIENT, 
    LeadContactConstants.STATUS_QUALIFIED, 
    LeadContactConstants.STATUS_CONTACTED, 
    LeadContactConstants.STATUS_PRISTINE 
] 
order_data = sorted(queryset, key = lambda p: order.index(p.status)) 
+0

不錯,這是更好的,謝謝你,讓我對這個玩了一會兒,您所提供的作品就像一個魅力的第一個解決方案 – PetarP