question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

KeyError: 'id' django-import-export

See original GitHub issue

I don’t know where it is wrong, right in my code, please help I already wasted lot of my time… THANK YOU _/\_

admin.py from import_export import resources from import_export.admin import ImportExportModelAdmin

class CapabilityResource(resources.ModelResource): model = Capability skip_unchanged = True report_skipped = True exclude = (‘id’,) import_id_fields = (‘capscode’, ‘capsid’, ‘capsname’, ‘cfor’, ‘parent’,)

@admin.register(Capability) class CapabilityAdmin(ImportExportModelAdmin): resource_class = CapabilityResource fields = [‘capscode’, ‘capsname’, ‘cfor’, ‘parent’] list_display = [‘capscode’, ‘capsname’, ‘cfor’, ‘parent’] list_display_links = [‘capscode’, ‘capsname’]

models.py class Capability(models.Model):
CFOR_CHOICES = [(‘WEB’, ‘WEB’), (‘PORTLET’, ‘PORTLET’), (‘REPORT’, ‘REPORT’), (‘MOB’, ‘MOB’)] capsid = models.AutoField(primary_key=True, auto_created=True, editable=False) capscode = models.CharField((‘caps’), max_length=50) capsname = models.CharField((‘includes’), max_length=1000, default = None, blank=True, null=True) parent = models.ForeignKey(‘self’, on_delete=models.RESTRICT, db_column=‘parent’, null=True, blank=True, related_name=‘children’) cfor = models.CharField(_(‘cfor’), max_length=10, default=‘WEB’, choices=CFOR_CHOICES) clientid = models.ForeignKey(‘onboarding.Bt’, null=True, blank=True, on_delete = models.RESTRICT, db_column=‘clientid’)

csv data: capscode capsid capsname cfor parent NONE 1 NONE WEB DASHBOARDS 2 DASHBOARDS WEB 1 RP_MONITORING 3 RP MONITORING WEB 2 FACILITY 4 FACILITY WEB 2

versions: django-import-export==2.6.0 Django==3.2.4 python=3.8.10

Errors:_ Line_ number: 1 - ‘id’ NONE, 1, NONE, WEB, Traceback (most recent call last): File “/home/xyz/abc/envs/intelliwiz_env/lib/python3.8/site-packages/import_export/resources.py”, line 650, in import_row instance, new = self.get_or_init_instance(instance_loader, row) File “/home/xyz/abc/envs/intelliwiz_env/lib/python3.8/site-packages/import_export/resources.py”, line 342, in get_or_init_instance instance = self.get_instance(instance_loader, row) File “/home/xyz/abc/envs/intelliwiz_env/lib/python3.8/site-packages/import_export/resources.py”, line 329, in get_instance import_id_fields = [ File “/home/xyz/abc/envs/intelliwiz_env/lib/python3.8/site-packages/import_export/resources.py”, line 330, in self.fields[f] for f in self.get_import_id_fields() KeyError: ‘id’

Line number: 2 - ‘id’ DASHBOARDS, 2, DASHBOARDS, WEB, 1 Traceback (most recent call last): File “/home/xyz/abc/envs/intelliwiz_env/lib/python3.8/site-packages/import_export/resources.py”, line 650, in import_row instance, new = self.get_or_init_instance(instance_loader, row) File “/home/xyz/abc/envs/intelliwiz_env/lib/python3.8/site-packages/import_export/resources.py”, line 342, in get_or_init_instance instance = self.get_instance(instance_loader, row) File “/home/xyz/abc/envs/intelliwiz_env/lib/python3.8/site-packages/import_export/resources.py”, line 329, in get_instance import_id_fields = [ File “/home/xyz/abc/envs/intelliwiz_env/lib/python3.8/site-packages/import_export/resources.py”, line 330, in self.fields[f] for f in self.get_import_id_fields() KeyError: ‘id’

Line number: 3 - ‘id’ RP_MONITORING, 3, RP MONITORING, WEB, 2 Traceback (most recent call last): File “/home/xyz/abc/envs/intelliwiz_env/lib/python3.8/site-packages/import_export/resources.py”, line 650, in import_row instance, new = self.get_or_init_instance(instance_loader, row) File “/home/xyz/abc/envs/intelliwiz_env/lib/python3.8/site-packages/import_export/resources.py”, line 342, in get_or_init_instance instance = self.get_instance(instance_loader, row) File “/home/xyz/abc/envs/intelliwiz_env/lib/python3.8/site-packages/import_export/resources.py”, line 329, in get_instance import_id_fields = [ File “/home/xyz/abc/envs/intelliwiz_env/lib/python3.8/site-packages/import_export/resources.py”, line 330, in self.fields[f] for f in self.get_import_id_fields() KeyError: ‘id’

Line number: 4 - ‘id’ FACILITY, 4, FACILITY, WEB, 2 Traceback (most recent call last): File “/home/xyz/abc/envs/intelliwiz_env/lib/python3.8/site-packages/import_export/resources.py”, line 650, in import_row instance, new = self.get_or_init_instance(instance_loader, row) File “/home/xyz/abc/envs/intelliwiz_env/lib/python3.8/site-packages/import_export/resources.py”, line 342, in get_or_init_instance instance = self.get_instance(instance_loader, row) File “/home/xyz/abc/envs/intelliwiz_env/lib/python3.8/site-packages/import_export/resources.py”, line 329, in get_instance import_id_fields = [ File “/home/xyz/abc/envs/intelliwiz_env/lib/python3.8/site-packages/import_export/resources.py”, line 330, in self.fields[f] for f in self.get_import_id_fields() KeyError: ‘id’

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
naveens800commented, Sep 21, 2021

I already checked with version 2.5.0 , and the results are same!

0reactions
naveens800commented, Sep 28, 2021

Thank you.

Read more comments on GitHub >

github_iconTop Results From Across the Web

django-import-export: cannot exclude id field during ...
When I try to upload CSV without "id" field throws this error. Line number: 1 - u'id' 13173474, Harry McDade, 10.harry.asas@ ...
Read more >
Django import-export KeyError: 'id' · Issue #1267
https://stackoverflow.com/questions/67070462/django-import-export-error-while-importing-data-into-database-through-admin-pane.
Read more >
KeyError: 'id' self.fields[f] for f in self.get_import_id_fields() (#1)
Hi developer I am trying to import the excel file according to the resources.py file structure, but the error KeyError: 'id' has occurred....
Read more >
Getting started - Django import / export - Read the Docs
A field for a primary key field (in this case, id ) always needs to be present. In the rest of the code...
Read more >
django-import-export
A clear and concise description of what the bug is. On my import data file, my last column is a date field. On...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found