设定后台系统名
admin.AdminSite.site_header = ‘医院后台‘
添加自定义跳转按钮,添加新的列
#整体内容参考https://simpleui.72wo.com/docs/simpleui/quick.html#%E8%87%AA%E5%AE%9A%E4%B9%89%E6%8C%89%E9%92%AE-action
@admin.register(models.HospitalDepartmentModel)
class HospitalDepartmentModelAdmin(admin.ModelAdmin):
#添加新的列
def doctor_num(self, obj):
num = obj.doctormodel_set.all().__len__()
button_html = str(num)
return format_html(button_html)
doctor_num.short_description = ‘医生数量‘
list_display = ["id", "name", "department_description", "description","doctor_num"]
#添加自定义按钮
actions = [‘custom_button‘]
def custom_button(self, request, queryset):
pass
custom_button.type = ‘info‘
# 指定element-ui的按钮类型,参考https://element.eleme.cn/#/zh-CN/component/button
custom_button.short_description = ‘跳转百度‘
custom_button.action_type = 2
#按钮动作类型,0=当前页内打开,1=新tab打开,2=浏览器tab打开
custom_button.action_url = ‘http://www.baidu.com‘
通过django-import-export
实现导出功能
参考原文档:https://django-import-export.readthedocs.io/en/latest/installation.html#settings
- 安装:
pip install django-import-export
- 配置app,settings.py
INSTALLED_APPS = [ ... ‘import_export‘, ]
- 配置admin.py
from django.contrib import admin from main_app import models from import_export.admin import ImportExportModelAdmin # Register your models here. @admin.register(models.SleepAnalysisModel) class SleepAnalysisModelAdmin(ImportExportModelAdmin): list_display = ["id", "height", "weight", "age", "gender", "sleep_time", "have_snore", ]
- 访问管理页面并尝试导出