Admin Import broken if primary key is not "id"
See original GitHub issueDescribe 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)
- Add model using a
OneToOneField
withprimary_key=True
(example below) named anything BUT ‘id’ (Do not include a field called ‘id’ in model). - Add import to admin.py (example of each approach below)
- Run server
- Go to admin > new model > import
- Attempt import with csv content below.
- 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
- Add “id” field as blank IntegerField to model (just never expose it - the existence of the field with that name allows partial functionality).
- 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:
- Created a year ago
- Comments:13 (7 by maintainers)
Top GitHub Comments
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.
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. )