我正在尝试使用最近开源的django-import-export通过管理员导入一些数据,我需要选择自定义日期格式。
它有效,但有问题/更好的方法吗?
admin.py:
from import_export.admin import ImportExportMixin, ImportExportModelAdmin
from import_export import resources
from .models import Enrolment
class EnrolmentResource(resources.ModelResource):
class Meta:
model = Enrolment
fields = ('id','source','firstname','surname','date_of_birth',
'bonus','status','tracking_comments')
widgets = {
'date_of_birth': {'format': '%d/%m/%Y'},
}
class EnrolmentAdmin(ImportExportMixin, admin.ModelAdmin):
def get_resource_class(self):
if not self.resource_class:
return EnrolmentResource
else:
return self.resource_class
admin.site.register(Enrolment, EnrolmentAdmin)
答案 0 :(得分:9)
关注this代码示例,您应该执行以下操作:
# ...
class EnrolmentResource(resources.ModelResource):
# ...
class EnrolmentAdmin(ImportExportMixin, admin.ModelAdmin):
resource_class = EnrolmentResource
# ...
admin.site.register(Enrolment, EnrolmentAdmin)