I\'m trying to import data to one of my models, but it\'s failing because I\'m trying to upload the foreignKey Id, not the iterated number that import-export creates.
<There is ForeignKeyWidget in the documentation. You can use it here. There are also IntegerWidget and DecimalWidget.
from import_export.admin import ImportExportModelAdmin
class SolResource(resources.ModelResource):
school_id = fields.Field(
column_name='school_id',
attribute='school_id',
widget=ForeignKeyWidget(School, 'name'))
class Meta:
model = Sol
class SolAdmin(ImportExportModelAdmin):
list_display = ('name', 'school_id')
resources_class = SolResource
admin.site.register(Sol, SolAdmin)
This is a working example. Hope it will help.
Use the widget- works great.
school_id = fields.Field(column_name='school_id', attribute='Sol', widget=widgets.ForeignKeyWidget(Sol, 'school_id'))
i think it will help:
class SolResource(resources.ModelResource):
school_id = fields.Field()
class Meta:
# ...
def dehydrate_school_id(self, sol):
return sol.school_id.school_id # You can get all fields of related model. This is example.