I want to receive get query and filter dataset with query params (if present). I'm currently using dumb method listed below. In this case I don't like the fact it's not checking that dates is actually could be parsed. In another method I may want to receive only numeric string which could be parsed to int. Is there some cool pythonic way to do it without writing a bunch of boilerplate code?
class TrackList(APIView):
@token_required
def get(self, request, pk, **kwargs):
# read query params
date_from = self.request.query_params.get('date_from')
date_to = self.request.query_params.get('date_to')
# if present then filter
if date_from and date_to:
points = Track.objects.filter(user_id=pk, date__range=[date_from, date_to])
# otherwise don't filter
else:
points = Track.objects.filter(user_id=pk)
points.order_by('date')
serializer = TrackListSerializer(points, many=True)
return Response(serializer.data, status=status.HTTP_200_OK)
UPD: The question is not actually about dateutil.parser, it is about general query params parser. Maybe I should use Django rest serializers?