0

I am trying to import data from excel sheet to database using Django but its not working.

excel file is consist of 2 columns headers.

id -
name

Errors Line number: 1 - 'id' None, test Traceback (most recent call last): File "C:\Users\LTGM~1\Desktop\TESTFO~1\testanal\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\TESTFO~1\testanal\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\TESTFO~1\testanal\lib\site-packages\import_export\resources.py", line 263, in get_instance return instance_loader.get_instance(row) File "C:\Users\LTGM~1\Desktop\TESTFO~1\testanal\lib\site-packages\import_export\instance_loaders.py", line 31, in get_instance field = self.resource.fields[key] KeyError: 'id'

I tried the import_export module.

  1. create the class in models.py
  2. create the resource in resources.py
  3. create the admin in the admin.py
  4. create the function in the 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
# from .models import Mouhafazat


class criminal_type_resource(resources.ModelResource):
    class Meta:
        model = criminal_type

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 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')
Django Dg
  • 97
  • 4
  • 19
  • Possible duplicate of [Import data from excel spreadsheet to django model](https://stackoverflow.com/questions/38019267/import-data-from-excel-spreadsheet-to-django-model) – MagicKriss Feb 11 '19 at 10:32
  • Hello and welcome to SO. " its not working" is the most useless description of a problem. Instead, describe exactly what happens, and, in the case of an exception, post the _exact_ exception message __and__ the full traceback. Note that most of the time, carefully reading the exception message and traceback are enough to understand what went wrong, where, and why. – bruno desthuilliers Feb 11 '19 at 12:21
  • @brunodesthuilliers i had asked before a question with all the needed details ( the error and the traceback)but i did not get any answer so i delete it. – Django Dg Feb 11 '19 at 12:39
  • Giving less details is certainly not going to help, quite on the contrary... – bruno desthuilliers Feb 11 '19 at 12:42
  • so i will edit my question and add the details.hope that someone will have the answer or at least the guide. – Django Dg Feb 11 '19 at 12:45

0 Answers0