how to use Django filtered class data to seperate download view

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.

filters.py

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

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('CTA_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()
    cta_list = CTA.objects.all()
    cta_filter = CTAFilter(request.GET, queryset=cts_list)
    allcta = cta_filter.qs
    rows = allcta.values_list('id2','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

This download view even after passing the filtered class queryset giving me all model data.

Note: If I am Hardcoding 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.

Just one request - in the future, when you’re posting code, please copy/paste from the original rather than trying to retype it. The typos and mistakes make it a bit difficult to follow and even more difficult to identify what might be a problem and what’s just a transcription error.

For example:

This raises the question, is the reference to cts_list in the second line actually supposed to be a reference to the cta_list in the line above it?

I would have similar questions about other parts of the code, but rather than asking all of them, I’d rather see the real code you’re working with.

Hi KenWhitesell, Apologies for the mistake Please find the below code. when I am defining a separate function/view then the download(retrievects1_view) view is not working as it is downloading all the records from models.
So I have tried by keeping both logic in one view but no luck(then only one of them is working either filter OR download.)
It would be very helpful for me if you can guide or give me a hint where I am doing mistakes as I am a beginner in Django and posted the same question on Stackoverflow but did not get any suggestions.

Please find the below link.

    def retrievects_view(request):
    	if request.method == 'GET':
    		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
    		rows = allcts.values_list('id', 'AppleConnectID', 'Shift_timing', 'EmailID', 'Vendor_Company',
    		                          'Project_name', 'SerialNumber', 'Reason', 'last_updated_time')

    		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})

    def retrievects1_view(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', '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
    	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

So the reason why the first method is working and the second one is failing is because you’re never trying to actually execute the queryset in the first method.

You have this in the first method:

but you never use rows later on, which means Django never tries to evaluate it. I suspect that if you tried to use rows in your template you would see the same error.

As you’ve already seen, FilterSet.qs is not a queryset.

You could try applying the values_list clause to the queryset definition cts_list = CTS.objects.all().values_list(...) - I don’t know how that would work relative to the filter though. (I’m really not familiar with django_filter.)

At the very least, you could just remove the values_list clause and copy the fields you need as you iterate over the rows.

Hope this gives you some ideas.

Ken