Django Admin_export import module used for custom IMPORT

I tried following the official import-export doc:

https://django-import-export.readthedocs.org/en/latest/import_workflow.html#import-data-method-workflow

But I still don't know how to stick it to my admin , assuming that :

  • I want only a subset of fields (I created a resource model with the specified fields, but it still crashes when importing: KeyError full stack below.

  • Where is , in which the method is in my administrator class (inheriting the ImportExportModelAdmin course and using the specific resource_class ), I have to put the code responsible for some user actions that I want to perform after checking that the import data is correct, but before , inserting it into a database.

I am not very advanced in Django and will be grateful for some tips. An example of a working implementation will be appreciated, so if you know something like this in github-share.

+2
python django django-admin csv django-import-export
source share
1 answer

you can redefine it as

to create a new instance

 def get_instance(self, instance_loader, row): return False 

your user save

 def save_instance(self, instance, real_dry_run): if not real_dry_run: try: obj = YourModel.objects.get(some_val=instance.some_val) # extra logic if object already exist except NFCTag.DoesNotExist: # create new object obj = YourModel(some_val=instance.some_val) obj.save() def before_import(self, dataset, dry_run): if dataset.headers: dataset.headers = [str(header).lower().strip() for header in dataset.headers] # if id column not in headers in your file if 'id' not in dataset.headers: dataset.headers.append('id') 
0
source share

All Articles