0

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

模型.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)

设置.py

INSTALLED_APPS = (
    ...
    'import_export',
)

管理员.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)
4

2 回答 2

0

使用字段进行映射 字段表示对象字段与该字段表示之间的映射。

于 2017-06-24T08:43:16.543 回答
0

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

于 2017-08-10T10:04:37.067 回答