using other libraries forms, template and static files in my app

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.

library link:
https://django-import-export.readthedocs.io/en/latest/

sample code:

resource code:

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')[0]] 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

custom command:


class Command(BaseCommand):
    def handle(self, *args, **options):
        o = OfficeResource()
        o.run()

admin.py:

class OfficeAdmin(ImportExportModelAdmin):
    resource_classes = [OfficeResource]

admin.site.register(Office, OfficeAdmin)

There wouldn’t be anything “special” or unique about these features.

For example, a form is a form. You can import the form from a library to use in your view in exactly the same way you import one of your own forms - or any other library’s form. The same logic applies to the other components as well.

However, regarding the django-admin for non-admin users, don’t. Just don’t. See the first two paragraphs in the docs at The Django admin site | Django documentation | Django
Do yourself a favor and create your own views for this.

1 Like