how to use use Django filtered class data to seperate download view.

38 views
Skip to first unread message

Shailesh Yadav

unread,
Dec 21, 2020, 2:50:00 AM12/21/20
to django-filter
I am using Django filter and using it in normal view it is working as expected now I want to download the filtered data so for this I am writing one download view where I am trying to use the same FilterClass but no luck. It is giving me an ERROR(Exception Value:
type object 'CTSFilter' has no attribute 'values_list'
). Can anyone please help/suggest how to use filtered queryset in filter class more than one view OR pass the data of filtered query to the download views.
Please find my code.



class CTAFilter(django_filters.FilterSet):
id = django_filters.NumberFilter(label="DSID")
class Meta:
model = CTA
fields = ['id', 'EmailID','id','Shift_timing']

Here I want when the user will select Shift_timing for example Morning he will get 10 records so the same data I want to pass to the below download view. (For this I am using CTSFilter class but no luck.)

Please find the below download code(View).


def exportcts_data(request):
    response = HttpResponse(content_type='application/ms-excel')
    response['Content-Disposition'] = 'attachment; filename="CTA_ShiftTiming.xls"'
    wb = xlwt.Workbook(encoding='utf-8')
    ws = wb.add_sheet('CTS_ShiftChange Data') # this will make a sheet named Users Data
    # Sheet header, first row
    row_num = 0
    font_style = xlwt.XFStyle()
    font_style.font.bold = True
    columns = ['id','idk','Shift_timing','EmailID','Vendor_Company','Project_name','SerialNumber','Reason','last_updated_time']
    for col_num in range(len(columns)):
        ws.write(row_num, col_num, columns[col_num], font_style) # at 0 row 0 column
    # Sheet body, remaining rows
    font_style = xlwt.XFStyle()
    # cta_list = CTA.objects.all()
    # cta_filter = CTAFilter(request.GET, queryset=cta_list)
    # allcta = cta_filter.qs
    rows = CTAFilter.values_list('id', 'idk', 'Shift_timing', 'EmailID', 'Vendor_Company', 'Project_name',
                              'SerialNumber', 'Reason', 'last_updated_time')
    for row in rows:
        row_num += 1
        for col_num in range(len(row)):
            ws.write(row_num, col_num, row[col_num], font_style)
    wb.save(response)
    return response    


Note: If I am Hardcoding it it is working for example.

rows = `TCA.objects.filter(Shift_timing__exact='Morning').values_list('id','idk','Shift_timing','EmailID','Vendor_Company','Project_name','SerialNumber','Reason','last_updated_time')`

so above code give me all result where Shift timing is morning but I want to do it dynamically bypassing filtered class data. Any help on this would be highly appreciable.


Carlton Gibson

unread,
Dec 21, 2020, 2:53:08 AM12/21/20
to django...@googlegroups.com
Hi.


On 21 Dec 2020, at 08:49, Shailesh Yadav <shailesh...@gmail.com> wrote:

    # cta_filter = CTAFilter(request.GET, queryset=cta_list)
    # allcta = cta_filter.qs
    rows = CTAFilter.values_list('id', 'idk', 'Shift_timing', 'EmailID', 'Vendor_Company', 'Project_name',
                              'SerialNumber', 'Reason', 'last_updated_time’)

allcta.values_list() is likely what you want. 

CTAFilter is is FilterSet class. First, you need the instance of that — cta_filter — and then you need to access its queryset — qs — in order to call values_list(), which is a QuerySet method. 

Good luck. 
Carlton 

Shailesh Yadav

unread,
Dec 21, 2020, 3:18:47 AM12/21/20
to django-filter
Thanks carlton,

I have tried it but it is giving me all the model data but my requirement when the user applies filter he should get that data only.


def exportcts_data(request):
response = HttpResponse(content_type='application/ms-excel')
response['Content-Disposition'] = 'attachment; filename="CTS_ShiftTiming.xls"'

wb = xlwt.Workbook(encoding='utf-8')
ws = wb.add_sheet('CTS_ShiftChange Data') # this will make a sheet named Users Data
# Sheet header, first row
row_num = 0
font_style = xlwt.XFStyle()
font_style.font.bold = True
columns = ['id','ldap_id','Shift_timing','EmailID','Vendor_Company','Project_name','SerialNumber','Reason','last_updated_time']

for col_num in range(len(columns)):
ws.write(row_num, col_num, columns[col_num], font_style) # at 0 row 0 column
# Sheet body, remaining rows
font_style = xlwt.XFStyle()
cts_list = CTS.objects.all()
cts_filter = CTSFilter(request.GET, queryset=cts_list)
allcts = cts_filter.qs
rows = allcts.values_list('id2','AppleConnectID','Shift_timing','EmailID','Vendor_Company','Project_name','SerialNumber','Reason','last_updated_time')

for row in rows:
row_num += 1
for col_num in range(len(row)):
ws.write(row_num, col_num, row[col_num], font_style)
wb.save(response)
return response
return render(request, 'apple/CTSexport.html') # NO USE

Shailesh Yadav

unread,
Dec 21, 2020, 3:29:57 AM12/21/20
to django-filter
I am not sure why it is not working I have checked over stack overflow and document also but did not found any related post.Can you please help me to find out why called Quryset is not returning the filtered data?

Shailesh Yadav

unread,
Dec 21, 2020, 3:35:56 AM12/21/20
to django-filter

Shailesh Yadav

unread,
Dec 21, 2020, 4:43:13 AM12/21/20
to django-filter
Actually, I am using CTAFilter in my render view, and where I want when user filter result that should I get.

Shailesh Yadav

unread,
Dec 21, 2020, 5:29:34 AM12/21/20
to django-filter
If I am using in the same view It is after applying filter it is not showing result directly allowing to download result.

I think we need to write both views in one place but don't how to handle it. As if one is working other is breacking.

def retrievects_view(request):

if request.method == 'GET':

response = HttpResponse(content_type='application/ms-excel')
response['Content-Disposition'] = 'attachment; filename="CTS_ShiftTiming.xls"'
wb = xlwt.Workbook(encoding='utf-8')
ws = wb.add_sheet('CTS_ShiftChange Data') # this will make a sheet named Users Data
# Sheet header, first row
row_num = 0
font_style = xlwt.XFStyle()
font_style.font.bold = True
columns = ['id', 'AppleConnectID', 'Shift_timing', 'EmailID', 'Vendor_Company', 'Project_name', 'SerialNumber',
'Reason', 'last_updated_time']

for col_num in range(len(columns)):
ws.write(row_num, col_num, columns[col_num], font_style) # at 0 row 0 column
# Sheet body, remaining rows
font_style = xlwt.XFStyle()
cts_list = CTS.objects.all()
cts_filter = CTSFilter(request.GET, queryset=cts_list)
allcts = cts_filter.qs
print('All cts check in export:', allcts)
rows = allcts.values_list('id', 'AppleConnectID', 'Shift_timing', 'EmailID', 'Vendor_Company',

'Project_name', 'SerialNumber', 'Reason', 'last_updated_time')

for row in rows:
row_num += 1
for col_num in range(len(row)):
ws.write(row_num, col_num, row[col_num], font_style)
wb.save(response)
return response
else:
allcts = CTS.objects.all()
allcts1 = allcts
allctsgen = allcts1.filter(Shift_timing__exact='General')
allctsmor = allcts1.filter(Shift_timing__exact='Morning')
allctseve = allcts1.filter(Shift_timing__exact='Evening')
allctstotal = allcts1.filter(Shift_timing__exact='Total')

# For filtering using 'django_filters',

cts_list = CTS.objects.all()
cts_filter = CTSFilter(request.GET, queryset=cts_list)
allcts = cts_filter.qs

paginator = Paginator(allcts, 50)
page_number = request.GET.get('page')
try:
allcts = paginator.page(page_number)
except PageNotAnInteger:
allcts = paginator.page(1)
except EmptyPage:
allcts = paginator.page(paginator.num_pages)
return render(request, 'apple/cts.html', {'allcts': allcts, 'cts_filter': cts_filter, 'allcts1': allcts1,
'allctsgen': allctsgen, 'allctsmor': allctsmor,
'allctseve': allctseve,
'allctstotal': allctstotal})

Shailesh Yadav

unread,
Dec 23, 2020, 7:44:02 AM12/23/20
to django-filter
Hi Carl Can you please help me with this.

Shailesh Yadav

unread,
Dec 23, 2020, 7:44:34 AM12/23/20
to django-filter
Carlton*
Reply all
Reply to author
Forward
0 new messages