'django-import-export error while importing data into database
And this is the SS of CSV file
Line number: 1 - 'id'
Traceback (most recent call last):
File "/home/carlmark/Desktop/Final/realtyredefine/env/lib/python3.6/site-
packages/import_export/resources.py", line 500, in import_row
instance, new = self.get_or_init_instance(instance_loader, row)
File "/home/carlmark/Desktop/Final/realtyredefine/env/lib/python3.6/site-
packages/import_export/resources.py", line 277, in get_or_init_instance
instance = self.get_instance(instance_loader, row)
File "/home/carlmark/Desktop/Final/realtyredefine/env/lib/python3.6/site-
packages/import_export/resources.py", line 266, in get_instance
self.fields[f] for f in self.get_import_id_fields()
File "/home/carlmark/Desktop/Final/realtyredefine/env/lib/python3.6/site-
packages/import_export/resources.py", line 266, in <listcomp>
self.fields[f] for f in self.get_import_id_fields()
KeyError: 'id'
models.py
class PropertyModel(models.Model):
Created_By = models.ForeignKey(User, default=1, null=True, on_delete=models.SET_NULL)
Created_Date = models.DateTimeField(default=timezone.now)
Serial_No = models.IntegerField()
Property_ID = models.CharField(primary_key=True, max_length=15)
Last_Updated_By = models.CharField(max_length=20, null=True, blank=True)
Last_Updated_On = models.DateTimeField(null=True, blank=True)
Hot = models.BooleanField(null=True, blank=True, default=False)
Shared_Website = models.BooleanField(null=True, blank=True, default=False)
MCG_Property_Code = models.CharField(max_length=20, null=True, blank=True)
File_Name = models.CharField(max_length=20, null=True, blank=True)
Picture_Attached = models.ImageField(upload_to='images', null=True, blank=True)
# Project_info
Land_Use = models.CharField(max_length=25, choices=land_choice, null=True, blank=True)
Construction_Completion_Date = models.DateTimeField(null=True, blank=True)
Project_Structure = models.CharField(max_length=25, choices=structure_choice, null=True, blank=True)
Project_ID = models.CharField(max_length=20, null=True, blank=True)
Project_Name = models.CharField(max_length=25, null=True, blank=True)
Possession_Status = models.CharField(max_length=20, choices=possession_choice)
Total_Plot_Area = models.CharField(max_length=10, null=True, blank=True)
Unit_Plot_Area = models.CharField(max_length=20, null=True, blank=True, choices=unit_choice)
Plot_No = models.CharField(max_length=10, null=True, blank=True)
Total_Floor = models.CharField(max_length=2, null=True, blank=True)
# Property Info
Property_Status = models.CharField(max_length=20, choices=prop_status_choice)
Property_For = models.CharField(max_length=20, choices=prop_for_choice)
Calling_Update = models.CharField(max_length=20, null=True, blank=True, choices=calling_choice)
Property_Type = models.CharField(max_length=15, choices=prop_type_choice)
Property_Sub_Type = models.CharField(max_length=25, choices=prop_sub_type_choice)
Property_Use = models.TextField()
# Address
Full_Address = models.TextField(null=True, blank=True)
Building_Name = models.CharField(max_length=20, null=True, blank=True)
Wing = models.CharField(max_length=20, null=True, blank=True)
Unit_No = models.CharField(max_length=20, null=True, blank=True)
Floor = models.CharField(max_length=20, null=True, blank=True)
Block = models.CharField(max_length=20, null=True, blank=True)
Landmark = models.CharField(max_length=20, null=True, blank=True)
# All Locality
All_Locality = models.TextField(null=True, blank=True)
Road = models.CharField(max_length=20, null=True, blank=True)
Colony = models.CharField(max_length=20, null=True, blank=True)
Locality_1 = models.CharField(max_length=20, null=True, blank=True)
Locality_2 = models.CharField(max_length=20, null=True, blank=True)
Sector = models.CharField(max_length=20, choices=sector_choice, null=True, blank=True)
Area = models.CharField(max_length=20, null=True, blank=True)
City = models.CharField(max_length=15, choices=city_choioce)
State = models.CharField(max_length=20, choices=state_choice)
Country = models.CharField(max_length=10, choices=country_choice)
Pin_Code = models.CharField(max_length=20, null=True, blank=True)
# Owner Details
Owner_ID = models.CharField(max_length=20, null=True, blank=True)
Owner_Full_Name = models.TextField(null=True, blank=True)
Owner_All_Contact = models.TextField(null=True, blank=True)
Owner_Source_Type = models.CharField(max_length=20, null=True, blank=True, choices=owner_source_choice)
Owner_Company_Name = models.CharField(max_length=20, null=True, blank=True)
Owner_Name = models.CharField(max_length=50)
Owner_Mobile = models.CharField(max_length=15)
Owner_Home_Landline = models.CharField(max_length=15, null=True, blank=True)
Owner_Office_Landline = models.CharField(max_length=15, null=True, blank=True)
Owner_Alternative_No1 = models.CharField(max_length=15, null=True, blank=True)
Owner_Alternative_No2 = models.CharField(max_length=15, null=True, blank=True)
Owner_Alternative_No3 = models.CharField(max_length=15, null=True, blank=True)
Owner_Email = models.EmailField(null=True, blank=True)
Owner_Address = models.TextField(null=True, blank=True)
Owner_Representative_Name = models.CharField(max_length=20, null=True, blank=True)
Owner_Representative_No = models.CharField(max_length=15, null=True, blank=True)
Owner_Caretaker_Name = models.CharField(max_length=20, null=True, blank=True)
Owner_Caretaker_No = models.CharField(max_length=15, null=True, blank=True)
Co_Owner_Name = models.CharField(max_length=20, null=True, blank=True)
Co_Owner_No = models.CharField(max_length=15, null=True, blank=True)
Owner_Comment = models.TextField(null=True, blank=True)
# Area
Super_Area_Min = models.CharField(max_length=10, null=True, blank=True)
Super_Area_Max = models.CharField(max_length=10)
Unit_Area = models.CharField(max_length=10, choices=unit_choice)
Maintainance_Charge_PSF = models.CharField(max_length=10, null=True, blank=True)
Maintainance_Charge_Min_Rs = models.CharField(max_length=10, null=True, blank=True)
Maintainance_Charge_Max_Rs = models.CharField(max_length=10, null=True, blank=True)
Efficiency = models.CharField(max_length=3, null=True, blank=True)
Covered_Area_Min = models.CharField(max_length=10, null=True, blank=True)
Covered_Area_Max = models.CharField(max_length=10, null=True, blank=True)
Expected_Rent_PSF = models.IntegerField()
Expected_Rent_Min_Rs = models.IntegerField()
Expected_Rent_Max_Rs = models.IntegerField()
For_Sale = models.BooleanField(null=True, blank=True, default=False)
Sale_Price_PSF = models.IntegerField()
Sale_Price_Min_Rs = models.IntegerField()
Sale_Price_Max_Rs = models.IntegerField()
# Aminities
All_Aminities = models.TextField(null=True, blank=True)
Aminities_1 = models.CharField(max_length=20, null=True, blank=True)
Aminities_2 = models.CharField(max_length=20, null=True, blank=True)
Aminities_3 = models.CharField(max_length=20, null=True, blank=True)
Aminities_4 = models.CharField(max_length=20, null=True, blank=True)
Aminities_5 = models.CharField(max_length=20, null=True, blank=True)
Aminities_6 = models.CharField(max_length=20, null=True, blank=True)
Aminities_7 = models.CharField(max_length=20, null=True, blank=True)
# Property Details
Vastu = models.CharField(max_length=15, null=True, blank=True, choices=vastu_choice)
Furnishing_Type = models.CharField(max_length=25, choices=furn_type_choice, null=True, blank=True)
Furnishing_Quality = models.CharField(max_length=15, null=True, blank=True, choices=furn_quality_choice)
Property_Available = models.CharField(max_length=20, choices=prop_avail_choice, null=True, blank=True)
Property_Remarks_Internal = models.TextField(null=True, blank=True)
Property_Remarks_External = models.TextField(null=True, blank=True)
# Furnishing Details
Furnishing_Detail = models.TextField(null=True, blank=True)
Property_Condition = models.CharField(max_length=20, null=True, blank=True)
Cabin_No = models.CharField(max_length=3, null=True, blank=True)
Workstation_No = models.CharField(max_length=5, null=True, blank=True)
Car_Parking_No = models.CharField(max_length=3, null=True, blank=True)
Conference_No = models.CharField(max_length=3, null=True, blank=True)
Pantry = models.CharField(max_length=3, null=True, blank=True)
Server_Room = models.CharField(max_length=3, null=True, blank=True)
Meeting_Room = models.CharField(max_length=3, null=True, blank=True)
Cubical = models.CharField(max_length=3, null=True, blank=True)
Keys_With = models.CharField(max_length=20, null=True, blank=True)
# eXtra
Internal_Comments = models.TextField(null=True, blank=True)
External_Comments = models.TextField(null=True, blank=True)
Assigned_Manger = models.CharField(max_length=20, choices=manager_choice)
Group = models.CharField(max_length=20, choices=group_choice)
Teams = models.CharField(max_length=20, choices=team_choice)
Source_Channel = models.CharField(max_length=20, choices=source_channel_choice)
Sub_Source_Channel = models.CharField(max_length=20, choices=sub_source_choice)
# Pre-rented
Pre_Rented = models.BooleanField(null=True, blank=True, default=False)
Company_Name = models.CharField(max_length=20, null=True, blank=True)
Pre_Rented_Area_Min = models.CharField(max_length=10, null=True, blank=True)
Pre_Rented_Area_Max = models.CharField(max_length=10, null=True, blank=True)
ROI = models.CharField(max_length=3)
Sale_Price_Pre_Rented_PSF = models.IntegerField()
Sale_Price_Pre_Rented_Min_Rs = models.IntegerField()
Sale_Price_Pre_Rented_Max_Rs = models.IntegerField()
Sale_Remark = models.TextField(null=True, blank=True)
# Lease Terms
Lease_Start = models.DateTimeField(null=True, blank=True)
Possession_Date = models.DateTimeField(null=True, blank=True)
Lease_Period = models.CharField(max_length=20, null=True, blank=True)
Security_Deposit = models.CharField(max_length=10)
Lock_in_Period = models.CharField(max_length=3)
Monthly_Rent_Min_Rs = models.CharField(max_length=10, null=True, blank=True)
Monthly_Rent_Max_Rs = models.CharField(max_length=10, null=True, blank=True)
Rent_Escalation = models.CharField(max_length=3)
Pre_Rented_Remark = models.TextField(null=True, blank=True)
# Remaining
Extra1 = models.CharField(max_length=20, null=True, blank=True)
Extra2 = models.CharField(max_length=20, null=True, blank=True)
Extra3 = models.CharField(max_length=20, null=True, blank=True)
Extra4 = models.CharField(max_length=20, null=True, blank=True)
Attachement = models.CharField(max_length=20, null=True, blank=True)
Video_Link = models.CharField(max_length=20, null=True, blank=True)
objects = PropertyManager()
class Meta:
verbose_name_plural = "Properties"
ordering = ['-Created_Date']
def __str__(self):
return self.Property_ID
admini.py
@admin.register(PropertyModel)
class PropertyAdmin(ImportExportModelAdmin):
list_display = ['Property_ID', 'Created_By', 'Last_Updated_By']
I'm trying to import data from a CSV file to my Django database but whenever I tried to import I got "KeyError: 'id'" although there's no "id" field in my model. I'm this issue only when I using PostgreSQL, there's no error when I import the same data in sqlite3.
Solution 1:[1]
I had a similar problem. It is that for import django import-export uses "id" by default. First, create a resources.py file - where you will declare settings for import and export. And as an exception, declare "id".
resources.py
from import_export import resources
from .models import *
class PropertyAdminResource(resources.ModelResource):
class Meta:
model = PropertyModel
exclude = ('id',)
admin.py
from .resources import *
@admin.register(PropertyModel)
class PropertyAdmin(ImportExportModelAdmin):
resource_class = PropertyAdminResource
list_display = ['Property_ID', 'Created_By', 'Last_Updated_By']
I highly recommend using resources.py - where you can modify the methods for import and export.
Solution 2:[2]
I had some errors like you and managed to solve them following this answer - https://github.com/django-import-export/django-import-export/issues/92#issuecomment-598009569
I changed from version 2.0.2 to 2.0.1, removed the custom primary key and migrated again
I hope this helps with your problem
Solution 3:[3]
Please read https://django-import-export.readthedocs.io/en/latest/getting_started.html#customize-resource-options
So for your your model you could do
class PropertyAdminResource(resources.ModelResource):
class Meta:
model = PropertyModel
import_id_fields = ('Property_ID',)
class PropertyModelAdmin(ImportExportModelAdmin):
resource_class = PropertyAdminResource
admin.site.register(PropertyModel, PropertyModelAdmin)
tested with django-import-export 2.7.0
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | David Grigoryev |
Solution 2 | runlevel0 |
Solution 3 | Nwawel A Iroume |