1π
β
To export both Student.name
and Student.lastname
you can directly reference a Foreign Key relation in the fields
parameter (docs):
class ClassResource(resources.ModelResource):
class Meta:
model = Class
fields = ('student__name', 'student__lastname')
This means that the column names will appear in your export as:
student__name
student__lastname
If you want the name to be different, you can directly declare a field:
name = Field(
column_name='name',
attribute='name',
widget=ForeignKeyWidget(Student, 'name'))
This will then appear in the export under name
.
π€Matthew Hegarty
Source:stackexchange.com