I am doing a table in React, fetching some data from my Django DB.
It has filters and I want them to call the API to get results if needed.
Thing is I have to duplicate a lot of lines whereas I am pretty sure there is a better way to do this.
Here is some part of the code:
if ss_value and not es_value and not iv_value and not timestamp:
queryset = DailyReport.objects.all().filter(station_departure=ss_value)
elif not ss_value and es_value and not iv_value and not timestamp:
queryset = DailyReport.objects.all().filter(station_arrival=es_value)
elif not ss_value and not es_value and iv_value and not timestamp:
queryset = DailyReport.objects.all().filter(is_virtual=iv_value)
elif not ss_value and not es_value and not iv_value and timestamp:
queryset = DailyReport.objects.all().filter(
Q(timestamp__range=(min_dt, max_dt)) | Q(upload_timestamp__range=(min_dt, max_dt)))
logger.debug(queryset)
elif ss_value and es_value and not iv_value and not timestamp:
queryset = DailyReport.objects.all().filter(station_departure=ss_value, station_arrival=es_value)
elif ss_value and not es_value and iv_value and not timestamp:
queryset = DailyReport.objects.all().filter(station_departure=ss_value, is_virtual=iv_value)
and it goes on and on.
Do you have any idea of a way to do it in a cleaner way ??
Thank you :)