Django 做多級搜尋&僅查詢所需列(得到所需列的query_set)&下載
阿新 • • 發佈:2018-12-17
def get_query_all(dic=None,values=None):
res = oneModel.objects.filter(**dic).values(*values)
return res
- 多級搜尋: 將搜尋項以字典格式傳入filter,注意解包 **
- 得到所需的列:將所需列以列表形式傳入values,得到一個字典列表[obj1,obj2,obj3]
實現下載功能:
obj = get_query_all(dic, header) buf = BytesIO() # from io import BytesIO import openpyxl appeal_wb = openpyxl.Workbook(encoding='utf-8',write_only=True) ws = appeal_wb.create_sheet() ws.append(list(header_dic.values())) for row in obj: ws.append(list(row.values())) appeal_wb.save(buf) file = buf.getvalue() response = HttpResponse(content_type='application/vnd.ms-excel') response['Content-Disposition'] = 'attachment;filename=' + 'RebatePool' + '.xls' response.write(file) return response