gpt4 book ai didi

django - 如何使用 Django-Import-Export 导入数据?

转载 作者:行者123 更新时间:2023-12-01 13:50:07 25 4
gpt4 key购买 nike

django-import-export 的文档关于如何配置管理员从电子表格导入有点弱。有人有完整的例子吗?

最佳答案

这不是一个完整的模块。但你可以理解它应该是怎样的。

资源.py 文件

from import_export import resources
from .models import edxUser

class edxUserResource(resources.ModelResource):
class Meta:
model = edxUser
#skip_unchanged = True
#report_skipped = True
#if you want to exclude any field from exporting
exclude = ('id','edx_anonymized_id')
fields = ('id', 'edx_id', 'edx_anonymized_id', 'edx_email', 'edx_name', 'time_created', 'created_by')
#Order of the export fields
export_order = ('edx_id', 'edx_email')

admin.py 文件
from import_export.admin import ImportExportModelAdmin
from django.contrib import admin
from .models import edxUser
from resources import edxUserResource


@admin.register(edxUser)
class edxUserAdmin(ImportExportModelAdmin):
resource_class = edxUserResource

模型.py 文件
from __future__ import unicode_literals
from django.conf import settings
from django.db import models

class edxUser(models.Model):
edx_id = models.IntegerField('edX user id', blank=True, null=True)
edx_anonymized_id = models.IntegerField("edX anonymized user id", blank=True, null=True)
edx_email = models.EmailField('edx user email', max_length=75, blank=True)
edx_name = models.CharField('edx name', max_length=75, blank=True, null=True)
time_created = models.DateField('Created time', blank=True, null=True)
created_by = models.OneToOneField(settings.AUTH_USER_MODEL, null=True, blank=True)

def __unicode__(self):
return str(self.edx_id)

关于django - 如何使用 Django-Import-Export 导入数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32483639/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com