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.

Admin Import broken if primary key is not "id"

See original GitHub issue

Describe the bug If a OneToOneField (I have not fully tested with other foreign key types) uses the foreign key as the primary_key and names it something other than “id,” import does not work. If I create a garbage field in the model and call it “id” the decorator works, but the mixin still does not work at all. I validated this by adding a simple model to the test application.

To Reproduce Steps to reproduce the behavior: (Starting with test application)

  1. Add model using a OneToOneField with primary_key=True (example below) named anything BUT ‘id’ (Do not include a field called ‘id’ in model).
  2. Add import to admin.py (example of each approach below)
  3. Run server
  4. Go to admin > new model > import
  5. Attempt import with csv content below.
  6. See error (note errors differ slightly):

Error with Mixin Approach

Line number: 1 - 'reader'
11, Bob Smith
Traceback (most recent call last):
File "c:\Users\...\test-django-import-export\temp-venv\lib\site-packages\import_export\resources.py", line 661, in import_row
instance, new = self.get_or_init_instance(instance_loader, row)
File "c:\Users\...\test-django-import-export\temp-venv\lib\site-packages\import_export\resources.py", line 353, in get_or_init_instance
instance = self.get_instance(instance_loader, row)
File "c:\Users\...\test-django-import-export\temp-venv\lib\site-packages\import_export\resources.py", line 340, in get_instance
import_id_fields = [
File "c:\Users\...\test-django-import-export\temp-venv\lib\site-packages\import_export\resources.py", line 341, in <listcomp>
self.fields[f] for f in self.get_import_id_fields()
KeyError: 'reader'

Error with Decorator Approach

Line number: 1 - 'id'
11, Bob Smith
Traceback (most recent call last):
File "c:\Users\...\test-django-import-export\temp-venv\lib\site-packages\import_export\resources.py", line 661, in import_row instance, new = self.get_or_init_instance(instance_loader, row)
File "c:\Users\...\test-django-import-export\temp-venv\lib\site-packages\import_export\resources.py", line 353, in get_or_init_instance
instance = self.get_instance(instance_loader, row)
File "c:\Users\...\test-django-import-export\temp-venv\lib\site-packages\import_export\resources.py", line 340, in get_instance
import_id_fields = [
File "c:\Users\...\test-django-import-export\temp-venv\lib\site-packages\import_export\resources.py", line 341, in <listcomp>
self.fields[f] for f in self.get_import_id_fields()
KeyError: 'id'
Workaround:

Note: I don’t think this is a reasonable long-term solution, but, in case someone runs into this bug, it gets you moving again

  1. Add “id” field as blank IntegerField to model (just never expose it - the existence of the field with that name allows partial functionality).
  2. Add import to admin.py using decorator Mixin approach is still broken

Versions (please complete the following information):

  • Django Import Export: 2.8.0 (tested with pre-release with similar results)
  • Python 3.10.4
  • Django 4.1

Expected behavior Expect import to function fully when I set primary_key to be a field other than “id” and when I do not include a field with that name in my model.

##Code used for testing:

models.py

class Reader(models.Model):
    reader = models.OneToOneField(Book, on_delete=models.CASCADE, primary_key=True,)
    reader_name = models.CharField('Reader Name',max_length=100,blank=True, null=True)
    #id = models.IntegerField(blank=True, null=True)

admin.py

Decorator Approach

class ReaderImportExport(ImportExportModelAdmin):
    resource_class: ReaderResource

@admin.register(Reader)
class ReservationAdmin(ReaderImportExport):
    fields = ["reader","reader_name",]

Mixin Appproach

class ReaderAdmin(ImportExportMixin, admin.ModelAdmin):
    resource_class = ReaderResource

admin.site.register(Reader, ReaderAdmin)

full contents of import csv for test

reader,reader_name
11, Bob Smith

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:13 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
matthewhegartycommented, Aug 3, 2022

Yes that should be it. I will be able to clone that branch and hopefully find out what’s going on. I have limited time in the next couple of weeks so feel free to investigate yourself and submit a PR if you find a fix.

1reaction
joefessendencommented, Aug 3, 2022

I got it (but clarification is fine - I’m still new. I MIGHT try to figure out a fix, but I reached the end of my understanding - meta classes are not something I’ve actually set up. I just use them when a package has them. )

Read more comments on GitHub >

github_iconTop Results From Across the Web

Import model without id field as primary key with django ...
If the model is created and saved the key field is empty, I could see that this is because of import_export.resources.ModelResource.
Read more >
Violation of PRIMARY KEY constraint in SSMS import/export ...
Attempted to not import the surrogate ID - this fails as import wizard sets field to null if ignored instead of leaving empty....
Read more >
Inline admins are broken when primary key is not an AutoField ...
I was able to work around that issue and make the objects from the inline admin editable including their primary key by following...
Read more >
auto_increment flag repair on primary keys of a WordPress ...
Here's some code to restore the auto_increment flag on the primary keys of a WordPress MySQL database after a faulty export/import cycle.
Read more >
primary key is not shown in ModelView · Issue #52 - GitHub
The primary key appears in the list of the "User" menu (there is just a tiny problem with the columns when I try...
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