`filter_val1` and `filter_val2` are coming from POST request so they are in `string` format. But the `created_at` field of the `EXPENSES` table is in `datetime` format. This is why the result of the filter() is coming as empty.
The query will run fine when the 2 values compared are in the same format of data. Same like we can not add 3 apples and 2 bananas. Convert the `filter_val1` and `filter_val2` in datetime before passing them in the query. Refer: https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior On Sat, 20 Nov 2021 at 13:19, Eugene TUYIZERE <eugenetuyiz...@gmail.com> wrote: > Dear Ramadhan, > > Still it does not work > > On Sat, 20 Nov 2021 at 09:18, ramadhan ngallen <ngall...@gmail.com> wrote: > >> Depends on your query filter_val2 should lower than filter_val1 as the >> range start from val2 to val1. >> Otherwise change your parameter from val1 to val2 >> EXPENSES.objects.filter(Q(created_at__range=[filter_val2,filter_val1])) >> >> On 20 Nov 2021, 10:04 +0300, Eugene TUYIZERE <eugenetuyiz...@gmail.com>, >> wrote: >> >> Dear Team, >> >> I am trying to export filtered data from a django model, but I get an >> empty file. But when I remove the filter and add all, it works. >> >> Kindly assist to know how to export filtered data. Below is my code >> >> def exportexpenes(request):filter_val1=request.POST.get("filter1") >> filter_val2=request.POST.get("filter2")response=HttpResponse(content_type >> ='text/csv')response['Content-Disposition'] = 'attachment; >> filename=expenses' + str(datetime.datetime.now())+'.csv'writer = csv. >> writer(response)writer.writerow(['Category','Beneficiary','Description','Operation >> Date','Amount','Account Number','Paymrnt Date','Status'])expes = EXPENSES >> .objects.filter(Q(created_at__range=[filter_val2,filter_val1]))# expes = >> EXPENSES.objects.all()for exp in expes:writer.writerow([exp.category,exp >> .beneficiary,exp.description,exp.date_of_operation,exp.amount,exp >> .account_number,exp.payment_date,exp.status])return response >> regards, >> -- >> *Eugene* >> >> -- >> 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+unsubscr...@googlegroups.com >> <https://mailto:django-users+unsubscr...@googlegroups.com>. >> To view this discussion on the web visit >> https://groups.google.com/d/msgid/django-users/CABxpZHuqV0Py%3D05eyu%3Dv%3DDFBm%2B1y9v-e2AbmVn2Bvj66jG8MZA%40mail.gmail.com >> <https://groups.google.com/d/msgid/django-users/CABxpZHuqV0Py%3D05eyu%3Dv%3DDFBm%2B1y9v-e2AbmVn2Bvj66jG8MZA%40mail.gmail.com?utm_medium=email&utm_source=footer> >> . >> >> -- >> 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+unsubscr...@googlegroups.com. >> To view this discussion on the web visit >> https://groups.google.com/d/msgid/django-users/93a559c8-bc77-44b7-8499-469db0f224e5%40Spark >> <https://groups.google.com/d/msgid/django-users/93a559c8-bc77-44b7-8499-469db0f224e5%40Spark?utm_medium=email&utm_source=footer> >> . >> > > > -- > *TUYIZERE Eugene* > > > > *Msc Degree in Mathematical Science* > > *African Institute for Mathematical Sciences (AIMS Cameroon)Crystal > Garden-Lime, Cameroon* > > Bsc in Computer Science > > *UR-Nyagatare Campus* > > Email: eugene.tuyiz...@aims-cameroon.org > eugenetuyiz...@gmail.com > > Tel: (+250) 7 88 26 33 38, (+250) 7 22 26 33 38 > > -- > 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+unsubscr...@googlegroups.com. > To view this discussion on the web visit > https://groups.google.com/d/msgid/django-users/CABxpZHvFzd3N1ur5%3DbzmDtJUUAF3BfjamMg7pra-RCL5ffjZCw%40mail.gmail.com > <https://groups.google.com/d/msgid/django-users/CABxpZHvFzd3N1ur5%3DbzmDtJUUAF3BfjamMg7pra-RCL5ffjZCw%40mail.gmail.com?utm_medium=email&utm_source=footer> > . > -- 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+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAGp2JVEzF_pNQEEt6f1XYsUZ07SqxdfHHSiYa_7rtHbt%2B1ny3g%40mail.gmail.com.