我使用过django-import-export包。并希望从格式的csv文件中获得相关的数据库,
roll_no,student_name,subject_1_mark,subject_2_mark,....subject_n_mark
我很难将主题标记分配到django ORM上的相应字段
示例:
#csv file
roll_no, student_name, FOCP, IT, OS, DS, DL
201,john,56,34,65,34,68
class Student(models.Model):
""" Store Student Information """
student_name = models.CharField(max_length=255)
roll_no = models.IntegerField()
class Subject(models.Model):
""" Store Subject Information """
subject_name = models.CharField(max_length=150, null=True, blank=True)
sub_code = models.CharField(max_length=255, unique=True, null=True,blank=True)
class Exam(models.Model):
""" Store Exam Information """
exam_type = models.CharField(max_length=150, null=True, blank=True)
exam_date = models.DateField(null=True, blank=True)
class Mark(models.Model):
""" Store Mark Information """
subject_name = models.ForeignKey(Subject, related_name='marks', null=True, blank=True)
exam = models.ForeignKey(Exam, related_name='marks', null=True, blank=True)
mark = models.IntegerField(null=True, blank=True)
INSTALLED_APPS = (
...
'import_export',
)
from django.contrib import admin
from import_export.admin import ImportExportModelAdmin
from import_export import resources
from .models import Subject
class SubjectResource(resources.ModelResource):
class Meta:
model = Subject
class SubjectAdmin(ImportExportModelAdmin, admin.ModelAdmin):
resource_class = SubjectResource
admin.site.register(Subject, SubjectAdmin)
答案 0 :(得分:0)
答案 1 :(得分:0)
我重写before_import
并对解决我问题的数据做了一些额外的逻辑。我在github上发布了问题https://github.com/django-import-export/django-import-export/issues/644#issuecomment-320966810