其他分享
首页 > 其他分享> > 【Django Admin】django-import-export, 导出数据+PRO

【Django Admin】django-import-export, 导出数据+PRO

作者:互联网

# settings.py


INSTALLED_APPS = [
    'import_export',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

 

 

 

# admin.py

from import_export.admin import ExportActionModelAdmin

@admin.register(UserInfo)
class UserInfoAdmin(ExportActionModelAdmin):

    list_display = ("DEMO")

    list_per_page = 10

    # 配置导出数据
    resource_class = UserinfoResource

 

 

 

#export_config.py

"""" 导出配置  """
from .cont import type_interest_choices
from .models import UserInfo
from import_export import resources, widgets
from import_export.fields import Field


# 账号状态 自定义返回结果 class is_staff_widget(widgets.BooleanWidget): def render(self, value, obj): """ 如果模型字段做了`null=True`的设置,这里就可以处理“真、假、未知”三种状态 """ if value == 0: return "激活" else: return '冻结' # 性别状态 自定义返回结果 class sex_widget(widgets.IntegerWidget): """ 如果模型字段做了`null=True`的设置,这里就可以处理“真、假、未知”三种状态 """ def render(self,value, obj): if value == 0: return "男生" else: return '女生' # 兴趣爱好状态 复选框配合PRO class interest_widget(widgets.CharWidget): """ 复选框的值: type_interest_choices = ( (0, '音乐'), (1, '运动'), (2, '舞蹈'), (3, '游戏'),) """ def render(self,value, obj): string = "" for index in value.split(","): if index: string = string + type_interest_choices[int(index)][1] + "," return string[0:-1] class UserinfoResource(resources.ModelResource): """ attribute: 表名字 column_name: 导出字段名字 widget: 自定义字段值返回值 判断自定义返回值 """ name = Field(attribute='name', column_name='姓名') head = Field(attribute='head', column_name='照片') sex = Field(attribute='sex', column_name='性别',widget=sex_widget()) interest = Field(attribute='interest', column_name='兴趣爱好',widget=interest_widget()) is_staff = Field(attribute='is_staff', column_name='账号状态',widget=is_staff_widget()) age = Field(attribute='age', column_name='年龄') score = Field(attribute='score', column_name='成绩') time = Field(attribute='time', column_name='时间') date = Field(attribute='date', column_name='日期') class Meta: model = UserInfo

 

 

图:

 

 

 

标签:widget,name,Field,Admin,attribute,PRO,django,column,import
来源: https://www.cnblogs.com/wanghong1994/p/16421671.html