Filter by date range

73 views
Skip to first unread message

tech george

unread,
Sep 9, 2022, 9:48:52 AM9/9/22
to django...@googlegroups.com
Hello friends!

I am trying to give users an easier way to filter data by date range.

My views.py code is as below, but unfortunately, it is hiding data without applying the filter whenever I try to use if, else statements.

Please advise what I might be doing wrong.

views.py
ef referralsReports(request):
if request.method=="POST":
fromdate = request.POST.get('fromdate')
todate = request.POST.get('todate')
searchresults = Prescription.objects.raw('select id,description,prescribe,ailment,ailment_2,ailment_3,'
'sickOff,referral,date_precribed,nurse_id,patient_id_id,'
'non_work_related_sickOff from pharmacy_prescription where date_precribed '
'between "'+str(fromdate)+'" and "'+str(todate)+'"')
return render(request, 'pharmacist_templates/reports/referrals_report.html', {"data": searchresults})

else:
displaydata = Prescription.objects.filter(nurse=request.user.pharmacist).order_by('-id')
return render(request, 'pharmacist_templates/reports/referrals_report.html', {"data":displaydata})

template

image.png


carlos

unread,
Sep 9, 2022, 11:11:35 AM9/9/22
to django...@googlegroups.com
Hello why use raw?
query_results = Prescription.objects.filter(date__range=[fromdate,todate])
if you have any problem with performance hit database use select_related or used m2m field use prefect_related

best!

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CADYG20Go5PXJRP-MGJ07M36ftSaQ6WJVjYg_p4UYB9UUWge-LA%40mail.gmail.com.


--
att.
Carlos Rocha

tech george

unread,
Sep 10, 2022, 1:27:26 AM9/10/22
to django...@googlegroups.com
Hello Carlos,

I have used the code below as you advised by when I filter the table comes blank:

query_results = Prescription.objects.filter(date__range=[fromdate,todate])

Regards,


Muhammad Juwaini Abdul Rahman

unread,
Sep 10, 2022, 1:41:36 AM9/10/22
to django...@googlegroups.com
What's your value of 'fromdate' and 'todate'?

tech george

unread,
Sep 10, 2022, 2:13:26 PM9/10/22
to django...@googlegroups.com
Hello Muhammad,

I'm choosing dates between 2022-07-01 and 2022-07-30 which have records in the db.

I hope i have answered you right.


carlos

unread,
Sep 10, 2022, 7:44:45 PM9/10/22
to django...@googlegroups.com
hello try debug 
query_results = Prescription.objects.filter(date__range=["2022-07-01","2022-07-30"])
len(query_results)
if come with records so, fromdate and todate is the problem!



--
att.
Carlos Rocha

Muhammad Juwaini Abdul Rahman

unread,
Sep 11, 2022, 2:47:52 AM9/11/22
to django...@googlegroups.com
Either there's no data for that month or your query is wrongly formatted. Probably you can try your query in the shell first.

tech george

unread,
Sep 12, 2022, 2:04:38 AM9/12/22
to django...@googlegroups.com
Hello,

I have tried debugging as below and it returned data;

search_results = Prescription.objects.filter(date_prescribed__range=["2022-07-01",2022-07-30"])

Is there another way i can filter the dates?


tech george

unread,
Sep 12, 2022, 2:09:37 AM9/12/22
to django...@googlegroups.com
I managed to solve the issue.

I was using the wrong keyword to fetch the dates.

Thanks for the support.

Muhammad Juwaini Abdul Rahman

unread,
Sep 12, 2022, 3:59:47 AM9/12/22
to django...@googlegroups.com

KPADE Kouami Antoine

unread,
Sep 12, 2022, 8:29:22 AM9/12/22
to django...@googlegroups.com
Hey friend

I think your problem is from


searchresults = Prescription.objects.raw('select id,description,prescribe,ailment,ailment_2,ailment_3,'
                                                 'sickOff,referral,date_precribed,nurse_id,patient_id_id,'
                                                 'non_work_related_sickOff from pharmacy_prescription where date_prescribed '

                                                 'between "'+str(fromdate)+'" and "'+str(todate)+'"')

please review the documentation

https://docs.djangoproject.com/en/4.1/topics/db/sql/

I hope I have answered your question

Reply all
Reply to author
Forward
0 new messages