再次用import-export导入一个模型
创建模型
创建Course_real模型,修改app的models.py,,为实际开课信息,其中主要难点在计算字段的设置,用函数定义了work_load字段,但是其调用存在一定问题,因此有设置了一个work_load_cul字段,并调用property(work_load)函数,才能取到正确的计算字段值。
Course_real模型与teacher模型进行外键关联ForeignKey。
class Course_real(models.Model):
teacher = models.ForeignKey(Teacher, blank=True, null=True, on_delete=models.CASCADE, verbose_name='教师')
course = models.CharField(max_length=40,verbose_name="课程名称")
classes = models.CharField(blank=True,null=True,max_length=200,verbose_name="上课班级")
period = models.CharField(blank=True,null=True,max_length=30,verbose_name="学期")
course_time = models.FloatField(blank=True,null=True,verbose_name="学时数J")
benefit_factor = models.FloatField(blank=True,null=True,verbose_name="效益系数R")
course_type_factor = models.FloatField(blank=True,null=True,verbose_name="课程类别系数K")
course_target_factor = models.FloatField(blank=True,null=True,verbose_name="教学对象系数S")
practice = models.CharField(blank=True,null=True,max_length=2,verbose_name="是否为集中实践")
stu_nums = models.IntegerField(blank=True,null=True,verbose_name="学生人数")
practice_weeks = models.FloatField(blank=True,null=True,verbose_name="实习周数J")
def work_load(self):
work_load = 0.0
if self.practice == '是':
work_load = round(( 1 + ( self.stu_nums - 15 ) * 0.05 ) * self.practice_weeks,1)
elif self.practice == '否':
work_load = round(self.course_time * self.benefit_factor * self.course_type_factor * self.course_target_factor,1)
return work_load
work_load.short_description = "工作量"
work_load_cul = property(work_load)
def __str__(self):
return self.course
class Meta:
unique_together = ("teacher", "course", "classes","period")
verbose_name = "开课信息"
verbose_name_plural = verbose_name
修改app内的resources.py文件
增加 Course_real 模型的import-export资源类
class CourseRealResource(resources.ModelResource):
teacher = Field(attribute="teacher", column_name="教师", widget=ForeignKeyWidget(Teacher, 'name'))
course = Field(attribute='course', column_name='课程名称')
classes = Field(attribute='classes', column_name='上课班级')
period = Field(attribute='period', column_name='学期')
course_time = Field(attribute='course_time', column_name='学时数J')
benefit_factor = Field(attribute='benefit_factor', column_name='效益系数R')
course_type_factor = Field(attribute='course_type_factor', column_name='课程类别系数K')
course_target_factor = Field(attribute='course_target_factor', column_name='教学对象系数S')
practice = Field(attribute='practice', column_name='是否为集中实践')
stu_nums = Field(attribute='stu_nums', column_name='学生人数')
practice_weeks = Field(attribute='practice_weeks', column_name='实习周数J')
class Meta:
model = Course_real
import_id_fields = ['teacher', 'course', 'classes', 'period',]
修改app的admin.py
在后台添加 Course_real 模型导入导出功能
class CourseRealAdmin(ImportExportModelAdmin):
resource_class = CourseRealResource
list_display = ['id', 'teacher', 'course', 'classes', 'course_time', 'practice', 'stu_nums', 'practice_weeks',
'work_load_cul']
ordering = ["id", ]
admin.site.register(Course_real, CourseRealAdmin)
并且引入相应类,包括Course_real和CourseRealResource
迁移数据
python manage.py makemigrations
python manage.py migrate
批量读入excel课程信息
登录管理员用户
导入课程信息