from django.contrib import admin # Register your models here. import xlwt from django.http import HttpResponse from dal import models # Register your models here. from openpyxl import Workbook class FundAdmin(admin.ModelAdmin): actions = ["export_as_excel"] list_display = ('title', 'name','type','unit','status','create_time') list_filter = ['title', 'name','type','unit','status','create_time',] def export_as_excel(self, request, queryset): print("queryset",queryset) meta = self.model._meta # Defines the file name in the form:. App model name class name Print ( " Meta " , Meta) FIELD_NAMES are = [field.name for Field in meta.fields] # model all the field names Response = the HttpResponse (the content_type = ' file application / MSExcel ' ) # define the response content type response [ ' the content-Disposition ' ] = F ' Attachment; filename = {} .xlsx Meta ' # define the data format in response to WB = the Workbook () # Create the Workbook WS = wb.active #Sheet table in the current active ws.append (FIELD_NAMES are) # model as the header field names written in the first row for obj in QuerySet: # traversing the selected object list Print (obj) for Field in FIELD_NAMES are: Data = [F ' { getattr (obj, Field)} ' for Field in FIELD_NAMES are] # text format model attribute value list composed ws.append (data) # written model attribute value wb.save (response) # stores the data in response to the contents of return response export_as_excel.short_description =' Export Excel ' # the operation of the display text in the admin admin.site.register (models.Fund, FundAdmin)