[Django]-Foreign Key in django migration using django-import-export

5👍

Either of these two lines work:

ISO2_id = fields.Field( widget=widgets.ForeignKeyWidget(Country, 'ISO2'))

or

ISO2_id = fields.Field(column_name='ISO2_id', attribute='ISO2', widget=widgets.ForeignKeyWidget(Country, 'ISO2'))

using just:

fields = ('ISO2', 'footprint')

gives error

django.db.models.fields.related.RelatedObjectDoesNotExist: CountryFootprint has no ISO2.

The coercing to Unicode error was caused by my not having a string returned from the unicode def:

def __unicode__(self):
    return self.ISO2

should have been

def __unicode__(self):
    return self.ISO2.name

so many coding problems solved by a good nights sleep!

Leave a comment