使用反向外键从模型导出数据

时间:2017-11-04 17:40:02

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

我正在尝试使用所有相关模型导出模型“类”。 使用ForeignKeyWidget导出教师数据。但是如何将所有学生的数据(姓名,年龄)纳入出口?

型号:

class Teacher(model.Model):
    Name = models.CharField(max_length=100)
    Age = models.PositiveIntegerField()
    Subject = models.CharField(max_length=100)

class Student(model.Model);
    Name = models.CharField(max_length=100)
    Age = models.PositiveIntegerField()
    Class = models.ForeignKey(Class)

class Class(model.Model):
    Name = models.CharField(max_length=5)
    Level = PositiveIntegerField()
    Teacher = models.ForeignKey(Teacher)

管理:

class ClassResource(resources.ModelResource):
    Teacher_Name = fields.Field(
        column_name = 'Teacher',
        attribute = 'Teacher',
        widget = ForeignKeyWidget(Teacher, 'Teacher'))

    class Meta:
        model = Class
        fields = {'Name', 'Level', 'Teacher_Name'}

0 个答案:

没有答案