fix error KeyError: 'id' in import_export django package while importing excel file

Refresh

February 2019

Views

4 time

0

i am trying to import excel file into sqlite 3 database using import_export package in django.

excel file consiste of 2 columns :

  • id:(empty values)
  • name:(contains values)

its not working and display the below error:

Errors Line number: 1 - 'id'

None, smugling

Traceback (most recent call last): File "C:\Users\LTGM~1\Desktop\TEHLIL~2\TEHLIL~1\lib\site-packages\import_export\resources.py", line 492, in import_row instance, new = self.get_or_init_instance(instance_loader, row) File "C:\Users\LTGM~1\Desktop\TEHLIL~2\TEHLIL~1\lib\site-packages\import_export\resources.py", line 269, in get_or_init_instance instance = self.get_instance(instance_loader, row) File "C:\Users\LTGM~1\Desktop\TEHLIL~2\TEHLIL~1\lib\site-packages\import_export\resources.py", line 263, in get_instance return instance_loader.get_instance(row) File "C:\Users\LTGM~1\Desktop\TEHLIL~2\TEHLIL~1\lib\site-packages\import_export\instance_loaders.py", line 31, in get_instance field = self.resource.fields[key] KeyError: 'id'

Line number: 2 - 'id'

None, kidnap

Traceback (most recent call last): File "C:\Users\LTGM~1\Desktop\TEHLIL~2\TEHLIL~1\lib\site-packages\import_export\resources.py", line 492, in import_row instance, new = self.get_or_init_instance(instance_loader, row) File "C:\Users\LTGM~1\Desktop\TEHLIL~2\TEHLIL~1\lib\site-packages\import_export\resources.py", line 269, in get_or_init_instance instance = self.get_instance(instance_loader, row) File "C:\Users\LTGM~1\Desktop\TEHLIL~2\TEHLIL~1\lib\site-packages\import_export\resources.py", line 263, in get_instance return instance_loader.get_instance(row) File "C:\Users\LTGM~1\Desktop\TEHLIL~2\TEHLIL~1\lib\site-packages\import_export\instance_loaders.py", line 31, in get_instance field = self.resource.fields[key] KeyError: 'id'

i follow these steps :

  1. create class in models.py
  2. create resources in resources.py
  3. create class admin in admin.py
  4. create function in views.py

models.py

from django.db import models

    # Create your models here.

    class criminal_type(models.Model):
        criminal_typeID = models.AutoField(primary_key=True)
        name = models.CharField(max_length=10)

        def __str__(self):
            return str(self.name)

resources.py

from import_export import resources
from .models import criminal_type


class criminal_type_resource(resources.ModelResource):
    class Meta:
        model = criminal_type
        skip_unchanged = True
        report_skipped = True
        exclude =('id')
        import_id_fields = ('name')

admin.py

from import_export.admin import ImportExportModelAdmin
from django.contrib import admin
from .models import criminal_type

@admin.register(criminal_type)
class CriminalTypeAdmin(ImportExportModelAdmin):
    pass

views.py

from django.shortcuts import render
from tablib import Dataset


# Create your views here.

def home(request):
    return render(request,'home.html')


def simple_uploadCriminalType(request):
    if request.method == 'POST':
        criminalType = criminal_type_resource()
        dataset = Dataset()
        newCriminalType = request.FILES['myfile']

        imported_data = dataset.load(newCriminalType.read(),format="xlsx")
        result = criminalType.import_data(dataset, dry_run=True)  # Test the data import

        if not result.has_errors():
            criminalType.import_data(dataset, dry_run=False)  # Actually import now

    return render(request, 'core/simple_upload.html')

i expect to import the existing data in the excel file into the database in django admin site.

0 answers