def export_thirty_days(self, request, queryset):
opts = self.model._meta
file_name = "Sms Statistic Report"
sql_query = '''SELECT
COUNT(id) AS No_Of_Report,
vendor,
country_code,
SUM(new_code)*100/SUM(sent) AS 'failure_rate',
SUM(case when new_code =0 then 1 ELSE 0 end)*100/sum(sent) AS 'success_rate'
FROM sms_statistics
WHERE FROM_UNIXTIME(date) >= curdate() - interval 30 day
GROUP BY vendor, country_code
ORDER BY COUNT(id) DESC;'''
field_names = ('No of report', 'Vendor', 'Country Code', 'Failure Rate', 'Success Rate')
wb = Workbook()
ws = wb.active
ws.append(ExportExcelAction.generate_header(self, self.model, field_names))
with connection.cursor() as cursor:
cursor.execute(sql_query)
for row in cursor.fetchall():
l = list(row)
ws.append(l)
ws = style_output_file(ws)
response = HttpResponse(content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
response['Content-Disposition'] = f'attachment; filename={file_name}.xlsx'
wb.save(response)
return response
export_thirty_days.short_description = "Reports of Past 30 days"
export_thirty_days.acts_on_all = True
This is the solutions i've found to make it work
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…