Django==2.2.x
django-import-export==2.3.0
from django.contrib import admin
from .models import *
from .forms import *
from rangefilter.filter import DateRangeFilter, DateTimeRangeFilter
from import_export import resources, fields
from import_export.admin import ImportExportModelAdmin
from import_export.admin import ImportExportActionModelAdmin
from import_export.admin import ExportActionMixin
from import_export.widgets import ForeignKeyWidget
# from .utils import *
import datetime
import tablib
# Register your models here.
class InvestProjectResource(resources.ModelResource):
"""
导出 InvestProject 部分数据
"""
# Book.name.field.verbose_name 取模型的 verbose_name 名称
# name = fields.Field(attribute='name', column_name=Book.name.field.verbose_name)
fund_manager = fields.Field(
column_name='基金经理',
attribute='fund_manager',
widget=ForeignKeyWidget(FundManager, 'name'))
# 新增字段:fund_performance_3y
fund_performance_3y = fields.Field(column_name="收益率·近3年")
@staticmethod
def dehydrate_fund_performance_3y(instance: InvestProject):
# dehydrate_新字段名称,dehydrate_ 是固定的开头
# instance:InvestProject 的单个实例
rate_filter = FundPerformance.objects.filter(period="近3年", fund=instance)
if rate_filter.exists():
rate = rate_filter.first().return_rate
else:
rate = "-"
return rate