class Value_A(admin.ModelAdmin): list_display = ('name',"href_requests","href_link") list_filter = ['name',] # 定义 def get_form(self, request, obj=None, **kwargs): form = super(Value_A, self).get_form(request, obj, **kwargs) form.base_fields['link'].widget.attrs['style'] = ' 1000px;' form.base_fields['name'].widget.attrs['style'] = ' 1000px;' form.base_fields['text'].widget.attrs['cols'] = '100' form.base_fields['text'].widget.attrs['rows'] = '30' form.base_fields['text'].widget.attrs['style'] = ' 1000px;' return form formfield_overrides = { models.Value_A: {'widget': Textarea(attrs={'rows': 10, 'cols': 50})}, } list_per_page = 20 actions = ["export_as_excel"] def export_as_excel(self, request, queryset): meta = self.model._meta # 用于定义文件名, 格式为: app名.模型类名 field_names = [field.name for field in meta.fields] # 模型所有字段名 response = HttpResponse(content_type='application/msexcel') # 定义响应内容类型 response['Content-Disposition'] = f'attachment; filename={meta}.xlsx' # 定义响应数据格式 wb = Workbook() # 新建Workbook ws = wb.active # 使用当前活动的Sheet表 ws.append(field_names) # 将模型字段名作为标题写入第一行 for obj in queryset: # 遍历选择的对象列表 for field in field_names: data = [f'{getattr(obj, field)}' for field in field_names] # 将模型属性值的文本格式组成列表 ws.append(data) wb.save(response) return response export_as_excel.short_description = '导出Excel' # 该动作在admin中的显示文字 admin.site.register(models.Value_A, Value_A)