djangodjango-import-export

django-import-export "Errors: line number : 1 - 'id' " - "KeyError: id"


I have a model for driving schools in Denmark. When I try to import them form and .xlsx file, I get the following error:

Errors
Line number: 1 - 'id'
43959328, 2023-03-20, None, Autoskolen.com ApS, Langgade 3 - 4262 Sandved, 4262, Sandved, Anpartsselskab, 855300 Køreskoler, None, None, Nej, None, None, 1
Traceback (most recent call last):
File "C:\PATH\lib\site-packages\import_export\resources.py", line 713, in import_row
instance, new = self.get_or_init_instance(instance_loader, row)
File "C:\PATH\lib\site-packages\import_export\resources.py", line 375, in get_or_init_instance
instance = self.get_instance(instance_loader, row)
File "C:\PATH\lib\site-packages\import_export\resources.py", line 362, in get_instance
import_id_fields = [
File "C:\PATH\lib\site-packages\import_export\resources.py", line 363, in <listcomp>
self.fields[f] for f in self.get_import_id_fields()
KeyError: 'id'

I am using the import_export package for the import.

This is how my code looks like:

admin.py:

from import_export.admin import ImportExportModelAdmin
from django.contrib import admin
from .models import Koereskoler #Post

class KoereskoleAdmin(ImportExportModelAdmin, admin.ModelAdmin):
    ...

admin.site.register(Koereskoler, KoereskoleAdmin)

models.py:

from django.db import models
from datetime import date
from django.core.validators import RegexValidator
from django.utils import timezone
from django.contrib.auth.models import User

def default_user():
    return User.objects.get(id=1).pk


class Koereskoler(models.Model):
    **CVR_nummer = models.IntegerField(unique=True, primary_key=True, default=00000000)**
    startdato = models.DateField(default=timezone.now)
    ophoersdato = models.CharField(max_length=255, blank=True, null=True)
    navn = models.CharField(max_length=255)
    adresse = models.CharField(max_length=255)
    postnr = models.IntegerField(default=0000)
    by = models.CharField(max_length=50, null=True)
    virksomhedsform_type_choices = (
        ('Enkeltmandsvirksomhed', 'Enkeltmandsvirksomhed'),
        ('Anpartsselskab', 'Anpartsselskab'),
        ('Aktie', 'Aktie'),
        ('Interessentskab', 'Interessentskab'),
        ('Andelsselskab (-forening) med begrænset ansvar', 'Andelsselskab (-forening) med begrænset ansvar'),
        ('Selskab med begrænset ansvar', 'Selskab med begrænset ansvar'),
        ('Personligt ejet Mindre Virksomhed', 'Personligt ejet Mindre Virksomhed'),
        ('Forening', 'Forening'),
        ('Forening med begrænset ansvar', 'Forening med begrænset ansvar'),
    )
    virksomhedsform = models.CharField(blank=True, choices=virksomhedsform_type_choices, max_length=255)
    hovedbranche = models.CharField(max_length=50,default=855300)
    telefonnr = models.CharField(max_length=20, blank=True, null=True, validators=[RegexValidator('[12345679+/]')])
    email = models.EmailField(max_length=255, blank=True, null=True)
    reklamebeskyttet_type_choices = (
        ('Ja', 'Ja'),
        ('Nej', 'Nej'),
    )
    reklamebeskyttet = models.CharField(max_length=3, choices=reklamebeskyttet_type_choices)
    pris = models.PositiveIntegerField(blank=True,null=True)
    url = models.URLField(blank=True, null=True)
    owner = models.ForeignKey(User, default=default_user, on_delete=models.SET_DEFAULT, null=False)

    def __str__(self):
        return self.navn

I know, I do not have an ID field and that I've changed the default primary key to "CVR_nummer". This number is an 8 digit number every company in Denmark has.

when a manually create an object from the /admin site, and try to export it, it works fine and there isn't any id column. When I try to import the very same file, it give me the id-error.

The problem is, that I do not know how to escape the error.


Solution

  • So I figuered it out:

    on (in my case) line 76 in the resources in the import-export folder C:\PATH\Lib\site-packages\import_export\resources.py you have to define which modelfield is the PrimaryKey, if other than the default "id":

    Here you can see a screenshot of the resources.py file and the import_id_fields where the new PK is set