[Fixed]-Chain filter query params

1👍

If you need QuerySet as a result, try with queryset = queryset.filter(next_action_date__gte=filter_date).order_by('status'), but it probably won’t be your desired order.

But if you need just a filtered, sorted list (not a QuerySet), you can apply filter first, then get contacts by status and chain them all together.

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

EDIT

I have found a way nicer solution 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))

Leave a comment