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