链过滤查询参数

时间:2017-04-26 11:59:42

标签: django django-rest-framework django-queryset

我知道这是一个知情主题,但是听我说,我目前在我的视图中过滤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显示我的联系人,现在我有了这个自定义数据结构,我需要将其添加到此过滤器中,以便显示我的联系人,从最重要的到不重要的,我&#39 ;我试图将它添加到过滤器但我没有得到任何数据,所以我怎样才能恰当地完成这项工作,有人能告诉我吗?

这是我的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

1 个答案:

答案 0 :(得分:1)

如果您需要QuerySet,请尝试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))