1π
β
In a simple way you can override .filter_queryset method and use the range lookup to filter dates.
views.py
class ReportViewSet(viewsets.ModelViewSet):
queryset = Report.objects.all()
serializer_class = ReportSerializer
def filter_queryset(self, queryset):
date_after = self.request.GET.get('date_after', None)
date_before = self.request.GET.get('date_before', None)
if date_after and date_before:
queryset = queryset.filter(created_date__range=[date_after, date_before])
return super().filter_queryset(queryset)
endpoint
http://localhost:8000/reports/?date_after=yyyy-mm-dd&date_before=yyyy-mm-dd
Or, use one of the listed Third party packages. Personally I have tried django-rest-framework-filters, but the project is not updated since 2020 and that will cause problems with the latest versions of Django due to the removal of some packages.
π€Niko
Source:stackexchange.com