I am trying to import data into one of my models, but it fails because I am trying to load the foreignKey identifier, and not the iterated number that the import-export creates.
models.py
from django.db import models from import_export import resources class School(models.Model): name = models.CharField(max_length=100) slug = models.CharField(max_length=100) school_id = models.IntegerField(unique=True) class Sol(models.Model): school_id = models.ForeignKey(School, to_field='school_id') name = models.CharField(max_length = 100) Al1EOC = models.DecimalField(max_digits=5, decimal_places=2) AL2EOC = models.DecimalField(max_digits=5, decimal_places=2)
My admin.py
from import_export.admin import ImportExportModelAdmin class SolAdmin(ImportExportModelAdmin): list_display = ('name', 'school_id') resources_class = SolResource pass admin.site.register(Sol, SolAdmin)
My .csv details
id, name, school_id, Al1EOC, AL2EOC ,'Main st school', 1238008, 12.9, 14.9
When I export data from the SOL model, I get an iterated number for the school identifier. I want the actual school identifier to be the one that contains the foreignKey relationship. And, subsequently, I need to load data with this foreignKey number. I know that a ForeignKey widget is a way to do this, but I donβt understand how it is implemented.
source share