I use django-import-export library for managing importing large Excel files in django models. the mechanism is to write custom Resource classes (extending ModelResources from import_export) and overwrite needed methods. everything is working fine using django-admin and custom Command. loading large Excel files in Models handling relations and … .however now I got a request to write Forms in my App so other users can perform exact upload/download as Admin users can in Admin panel.
the django-import-export has its own form classes. static files and templates which is being used by django-admin.
what is the standard and best way to use django-import-export static files, internal Forms and templates or django-admin to be used for non-admin users? cause what I need is exactly what Admin users have for non-admin users.
from import_export import resources class OfficeResource(resources.ModelResource): def __init__(self): self.company_filename = NEW_FILENAMES.get('office') self.office = \ self.get_dataset() super().__init__() class Meta: model = Office import_id_fields = ('dep_name',) store_instance = True # skip_unchanged = True # for full control overwrite skip_row() # report_skipped = False # whether skipped records appear in the import Result + Admin UI fields = ( 'division_name', 'dep_name', 'full_name', 'persian_name', ) def before_import_row(self, row, row_number=None, **kwargs): if row['division_name']: row['division_name'] = clean_string(row['division_name']) if row['dep_name']: row['dep_name'] = clean_string(row['dep_name']) if row['full_name']: row['full_name'] = clean_string(row['full_name']) super().before_import_row(row, row_number, **kwargs) def get_dataset(self): data = tablib.Dataset() file = DOCUMENT_DIR / self.company_filename with open(file, 'rb') as f: data.load(f, 'xlsx') return data def for_delete(self, row, instance): if row[NEW_HEADERS.get('office')] is None: return True super().for_delete(row, instance) def run(self): self.import_data(self.office, dry_run=True, raise_errors=True) result = self.import_data(self.office, dry_run=False, raise_errors=True) return result
class Command(BaseCommand): def handle(self, *args, **options): o = OfficeResource() o.run()
class OfficeAdmin(ImportExportModelAdmin): resource_classes = [OfficeResource] admin.site.register(Office, OfficeAdmin)