I'm trying to order contacts in table and I'm facing some problems, I found a nice solution in this SO question, maybe it's an over kill for this, but I'm trying to do this over restapi
so I'm only affecting my rest view with this, ok so this is my solution for this ordering:
filter_date = self.request.query_params.get('filter_date', None)
case_sql = '(case when status="Client" ' \
'then 1 when status="Contacted" ' \
'then 2 when status="Qualified" ' \
'then 3 when status="Virgin" then 4 end)'
if filter_date is not None:
queryset = queryset.filter(next_action_date=filter_date).extra(select={'status': case_sql},
order_by=['status'])
I'm doing this because I don't want to change my db field, like I said I only want to affect my rest view, so the question is, am I doing this filter wrong all this all setup is wrong by default?
Model fields:
status = models.CharField(max_length=10, choices=LeadContactConstants.STATUSES, default=LeadContactConstants.STATUS_PRISTINE)
and the choices for this field:
class LeadContactConstants(object):
STATUS_PRISTINE = "PRISTINE"
STATUS_CONTACTED = "CONTACTED"
STATUS_QUALIFIED = "QUALIFIED"
STATUS_CLIENT = "CLIENT"
STATUSES = ((STATUS_PRISTINE, "Virgin"),
(STATUS_CONTACTED, "Contacted"),
(STATUS_QUALIFIED, "Qualified"),
(STATUS_CLIENT, "Client"))