Exported filtered by date data in django

256 views
Skip to first unread message

Eugene TUYIZERE

unread,
Nov 20, 2021, 2:05:19 AM11/20/21
to django...@googlegroups.com
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


ramadhan ngallen

unread,
Nov 20, 2021, 2:18:20 AM11/20/21
to 'Maryam Yousaf' via Django users
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 <eugenet...@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...@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.

Eugene TUYIZERE

unread,
Nov 20, 2021, 2:49:53 AM11/20/21
to django...@googlegroups.com
Dear Ramadhan,

Still it does not work



--
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....@aims-cameroon.org
           eugenet...@gmail.com

Tel: (+250) 7 88 26 33 38, (+250) 7 22 26 33 38

ramadhan ngallen

unread,
Nov 20, 2021, 2:55:28 AM11/20/21
to 'Maryam Yousaf' via Django users
Debug by printing val1 and val2 before filtering if those vslues are dates(class) if true print expenses after filtering. Otherwise your date ranges does not contain s data as required
African Institute for Mathematical Sciences (AIMS Cameroon)Crystal Garden-Lime, Cameroon

Bsc in Computer Science
UR-Nyagatare Campus
Email: eugene....@aims-cameroon.org           eugenet...@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...@googlegroups.com.

Lalit Suthar

unread,
Nov 21, 2021, 1:34:54 AM11/21/21
to django...@googlegroups.com
`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

Twizerimana Emmanuel

unread,
Nov 21, 2021, 2:46:38 AM11/21/21
to django...@googlegroups.com
hy
I want to declare global variable that used in many functions in python
can you show me

ngal...@gmail.com

unread,
Nov 21, 2021, 5:35:56 AM11/21/21
to django...@googlegroups.com
  1. Declare it on your settings.py

#settings.py

FOO = “foo”

 

When you want to use it

e.g views.py

from django.conf import settings

FOO_VARIABLES = settings.FOO

 

  1. Declare on .env files(OR using set/export)

BAR = “bar”

To use it

import os

BAR_VARIALBE = os.getenviron.get(“BAR”)

 

 

Regards

 

Ngallen Ramadhan

T: +255 765 889 960 | M: +255 715 200 997

Emial: rama...@maycom.co.tz | Skype: ngallen2

Reply all
Reply to author
Forward
0 new messages