我正在尝试使用 django-import-export 导入 xls 文件。
这就是我所拥有的:
class CourseResource(resources.ModelResource):
course_number = fields.Field(column_name='Kursnummer', attribute='course_number', widget=CharWidget())
school_year = fields.Field(column_name='Schuljahr', attribute='school_year',
widget=ForeignKeyWidget(model=SchoolYear, field='name'))
#other fields....
class Meta:
model = Course
exclude = ('students')
export_order = ('id', 'course_number', 'school_year')
学年模型:
class SchoolYear(models.Model):
name = models.CharField(max_length=25, unique=True)
start_date = models.DateField(verbose_name="Beginn")
end_date = models.DateField(verbose_name="Ende")
def __str__(self):
return self.name
def __unicode__(self):
return self.name
课程模式:
class Course(models.Model):
course_number = models.CharField(max_length=25, verbose_name="Kursnummer")
school_year = models.ForeignKey(SchoolYear, default=30, verbose_name="Schuljahr")
#some other fields...
students = models.ManyToManyField(Student, verbose_name="Schüler", null=True, blank=True, related_name="courses")
class Meta:
unique_together = ('course_number', 'school_year',)
verbose_name = "Kurs"
verbose_name_plural = "Kurse"
managed = True
def __str__(self):
return "%s (%s)" % (self.course_number, self.school_year)
我的 excel 文件中的示例行如下所示(第二列是课程编号,第三列是学年名称,然后是更多字段):
... 7aMa 2015/2016 ...
当我尝试在 django 管理员中导入文件时,我收到以下错误消息:
Row number: 1 - Column 'school_year': invalid literal for int() with base 10: '2015/2016' [...]
Traceback (most recent call last):
File "/Users/amishelli/Dropbox/IMS/ims2/import_export/resources.py", line 441, in import_row
self.import_obj(instance, row, dry_run)
File "/Users/amishelli/Dropbox/IMS/ims2/import_export/resources.py", line 300, in import_obj
self.import_field(field, obj, data)
File "/Users/amishelli/Dropbox/IMS/ims2/import_export/resources.py", line 290, in import_field
field.save(obj, data)
File "/Users/amishelli/Dropbox/IMS/ims2/import_export/fields.py", line 112, in save
setattr(obj, attrs[-1], self.clean(data))
File "/Users/amishelli/Dropbox/IMS/ims2/import_export/fields.py", line 68, in clean
raise ValueError("Column '%s': %s" % (self.column_name, e))
ValueError: Column 'school_year': invalid literal for int() with base 10: '2015/2016'
我认为 SchoolYear 的 ForeignKey 的查找不起作用。我究竟做错了什么?