Sé que es un tema conocido, pero escúchame, actualmente estoy filtrando query_params en mi opinión de esta manera:
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 y estoy mostrando mis contactos por next_action_date , ahora tengo esta estructura de datos personalizada y necesito agregarla a este filtro para que muestre mis contactos desde los más importantes hasta los que no lo son, he intentado agregarlos al filtro pero no No obtengo ningún dato, entonces, ¿cómo puedo hacer esto de manera adecuada? ¿Puede alguien mostrarme?
Este es mi 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 querysetSi necesita QuerySet como resultado, intente con queryset = queryset.filter(next_action_date__gte=filter_date).order_by('status') , pero probablemente no sea el orden deseado.
Pero si solo necesita una lista ordenada y filtrada (no un QuerySet), puede aplicar el filtro primero, luego obtener los contactos por estado y encadenarlos todos juntos.
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_dataEDITAR
He encontrado una solución mucho mejor aquí .
order = [ LeadContactConstants.STATUS_CLIENT, LeadContactConstants.STATUS_QUALIFIED, LeadContactConstants.STATUS_CONTACTED, LeadContactConstants.STATUS_PRISTINE ] order_data = sorted(queryset, key = lambda p: order.index(p.status))