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.