Django Import-Export import error for one-to-one field – KeyError: 'id'

Question:

Using:
Python 3.10.4
Django 4.06
Django-import-export 2.8.0

I am trying to import data to use as demo data into my django application. I keep getting a KeyError.

### models.py
class Reservation(models.Model):
    reservation = models.OneToOneField(Vehicle, on_delete=models.CASCADE, primary_key=True,)
    delivered = models.BooleanField('Delivered',default=False)
    date_reserved = models.DateTimeField('date reserved', default=datetime.datetime.now)
    ...

### admin.py
class ReservationResource(resources.ModelResource):
    class Meta:
        model = Reservation
        exclude = ('id',)
        import_id_fields = ('reservation',)
        fields = (
            'reservation',
            'delivered',
            'date_reserved',
            ...
        )
class ReservationImportExport(ImportExportModelAdmin):
    resource_class: ReservationResource
@admin.register(Reservation)
class ReservationAdmin(SimpleHistoryAdmin, ReservationImportExport):
    fields = ["delivered","date_reserved",...]

### demo-reservations.yaml  (Note: Problem happens using different data file formats)
 - reservation: 50001
   delivered: False
   date_reserved: 7/15/2022T00:00:00+00:00
   ...

Here’s the error (slightly obfuscated)

Traceback (most recent call last):
File "c:Users...libsite-packagesimport_exportresources.py", line 661, in import_row
instance, new = self.get_or_init_instance(instance_loader, row)
File "c:Users...libsite-packagesimport_exportresources.py", line 353, in get_or_init_instance
instance = self.get_instance(instance_loader, row)
File "c:Users...libsite-packagesimport_exportresources.py", line 340, in get_instance
import_id_fields = [
File "c:Users...libsite-packagesimport_exportresources.py", line 341, in <listcomp>
self.fields[f] for f in self.get_import_id_fields()
KeyError: 'id'

Tried Already:

  1. Removed SimpleHistoryAdmin from Admin registration
  2. Put breakpoints in debugger – it is clear that it is ignoring the "import_id_fields" value. If I manually change the value to ‘reservation’ when it calls get_import_id_fields(self), I get further (a second issue I will ask separately – guessing stackoverflow wants 1 issue per question. someone feel free to let me know if lumping them works better.) I do see in the debugger that ‘id’ is the value of self._meta.import_id_fields when it gets to that call.
  3. Changing the model from OneToOne to ForeignKey

Every other answer I have managed to dig up seems to say that adding that exclude id and import_id_fields should have resolved this. The only think I have not tried (and really do not want to) is changing the id/foreign key column name.

EDIT 1:
At this point, I’m almost certain this is a bug in the django-import-export package. If there is field called "id" in the model, the admin import is broken. I’m a little over my head to fully troubleshoot.
I entered a bug over on GitHub. https://github.com/django-import-export/django-import-export/issues/1480

Asked By: JoeF

||

Answers:

In case this is found, just wanted to close the loop. I had an error in my code.

class ReservationImportExport(ImportExportModelAdmin):
    resource_class: ReservationResource

should have been:

class ReservationImportExport(ImportExportModelAdmin):
    resource_class = ReservationResource

That = instead of : made all the difference.

Answered By: JoeF