copser
copser

Reputation: 2641

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.

Upvotes: 0

Views: 885

Answers (2)

Sathish Kumar VG
Sathish Kumar VG

Reputation: 2172

There are two operators to do this one is less than or equal to(__lte)

As given below:

 queryset = queryset.filter(next_action_date__lte=filter_date)

Second one is less than (__le),

This will not give the current Filter Condition

 queryset = queryset.filter(next_action_date__lt=filter_date) 

Upvotes: 0

Abijith Mg
Abijith Mg

Reputation: 2693

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.

Upvotes: 1

Related Questions