如何将主题名称分配给其正确的字段

时间:2017-06-24 07:58:09

标签: django django-models django-import-export

我使用过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

Models.py

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)

settings.py

INSTALLED_APPS = (
    ...
    'import_export',
)

admin.py

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)

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

我重写before_import并对解决我问题的数据做了一些额外的逻辑。我在github上发布了问题https://github.com/django-import-export/django-import-export/issues/644#issuecomment-320966810