Django 链过滤器查询参数

Django 链过滤器查询参数,django,django-rest-framework,django-queryset,Django,Django Rest Framework,Django Queryset,我知道这是一个众所周知的主题,但请听我说,我目前在我的视图中对query\u 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 我通过下一个动作日期显示我的联系人,

我知道这是一个众所周知的主题,但请听我说,我目前在我的视图中对
query\u 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
我通过下一个动作日期显示我的联系人,现在我有了这个自定义数据结构,我需要将它添加到这个过滤器中,这样它将显示我的联系人,从最重要到不重要,我已经尝试将它添加到过滤器中,但我没有得到任何数据,所以我如何才能适当地完成这项工作,有人能告诉我吗

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

如果您需要QuerySet作为结果,请尝试使用
QuerySet=QuerySet.filter(下一步动作日期)。order by('status')
,但它可能不是您想要的订单

但如果您只需要一个经过筛选、排序的列表(而不是查询集),您可以先应用筛选,然后按状态获取联系人并将其链接在一起

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
编辑

我找到了一个更好的解决办法


结果必须是QuerySet吗?它能被分类吗,普通的对象列表?@MateuszKnapczyk好吧,我需要在下一个动作日期之前给我的联系人穿上鞋子,并且要正确地排序,我愿意接受建议,你可以给我看,你需要更多的数据吗?很好,这更好,谢谢,让我玩一会儿,你提供的第一个解决方案很有魅力
order = [
    LeadContactConstants.STATUS_CLIENT,
    LeadContactConstants.STATUS_QUALIFIED,
    LeadContactConstants.STATUS_CONTACTED,
    LeadContactConstants.STATUS_PRISTINE
]
order_data = sorted(queryset, key = lambda p: order.index(p.status))