Search code examples
djangodjango-rest-frameworkdjango-querysetdjango-rest-viewsets

django rest framework - filtering against date query parameter


In my view I'm receiving date parameter and I'm filtering against it so I could show my contact for today, and it goes something like this:

filter_date = self.request.query_params.get('filter_date', None)

for queryset in contact_lead:
   if filter_date is not None:
       queryset = queryset.filter(next_action_date__gte=filter_date)

return queryset

Like I said with this I can see my contacts for today, but there are some contact that are made in the past, now because datepicker have past dates restriction I can not see them and I want all my past contact to appear today, or any other day in the future, so the point is I don't want contact which are created in the past to be left behind, so can someone help me and explain how can I get that result.


Solution

  • Replace greater than or equal to(__gte) with less than or equal to(__lte) in the query lookup. As shown below:

    queryset = queryset.filter(next_action_date__lte=filter_date)
    

    This will fix the issue.