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.

Unable to export using ImportExportModelAdmin

See original GitHub issue

Using ‘import-export v==1.0.1’

This is my stack trace:

Traceback:

File "/Users/fmirdita/Projects/django/django/core/handlers/exception.py" in inner
35. response = get_response(request)

File "/Users/fmirdita/Projects/django/django/core/handlers/base.py" in _get_response
128. response = self.process_exception_by_middleware(e, request)

File "/Users/fmirdita/Projects/django/django/core/handlers/base.py" in _get_response
126. response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/Users/fmirdita/Projects/django/django/utils/decorators.py" in _wrapped_view
142. response = view_func(request, *args, **kwargs)

File "/Users/fmirdita/Projects/django/django/views/decorators/cache.py" in _wrapped_view_func
44. response = view_func(request, *args, **kwargs)

File "/Users/fmirdita/Projects/django/django/contrib/admin/sites.py" in inner
223. return view(request, *args, **kwargs)

File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/import_export/admin.py" in export_action
403. queryset = self.get_export_queryset(request)

File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/import_export/admin.py" in get_export_queryset
374. self.list_max_show_all, self.list_editable, self

Exception Type: TypeError at /admin/reception/person/export/
Exception Value: init() missing 1 required positional argument: 'sortable_by'

My ModelAdmin class inherits from ImportExportModelAdmin, and when using the admin interface I can click export and choose CSV from a dropdown. Upon submitting, I get error TypeError at /admin/reception/service/export/ __init__() missing 1 required positional argument: 'sortable_by'

Looking at ExportMixIn get_export_queryset it appears the error comes from this line: cl = ChangeList(request, self.model, list_display, list_display_links, list_filter, self.date_hierarchy, search_fields, self.list_select_related, self.list_per_page, self.list_max_show_all, self.list_editable, self )

From ChangeList docs: class ChangeList: def __init__(self, request, model, list_display, list_display_links, list_filter, date_hierarchy, search_fields, list_select_related, list_per_page, list_max_show_all, list_editable, model_admin, sortable_by):

I’m not sure why it’s just self at the end of of that line of the get_export_queryset method in the ExportMixIn class. It seems there might be a missing argument for this line?

What else I’ve tried: In my ModelAdmin I put resource_class = ModelResource and wrote a corresponding ModelResource for each of my models. I was able to run

>>> from app.admin import ModelResource
>>> dataset = ModelResource.export()
>>> print(dataset.csv)
-printed correct model data-

So clearly the export functionality works, but not with the admin interface. Thanks for the help!

Update: Found a temporary fix by overriding the get_export_queryset function in the ModelAdmin that inherits the ImportExportModelAdmin class. I just passed in None:

    def get_export_queryset(self, request):
            """
            Returns export queryset.
            Default implementation respects applied search and filters.
            """
            list_display = self.get_list_display(request)
            list_display_links = self.get_list_display_links(request, list_display)
            list_filter = self.get_list_filter(request)
            search_fields = self.get_search_fields(request)
            if self.get_actions(request):
                list_display = ['action_checkbox'] + list(list_display)

            ChangeList = self.get_changelist(request)
            cl = ChangeList(request, self.model, list_display,
                            list_display_links, list_filter, self.date_hierarchy,
                            search_fields, self.list_select_related, self.list_per_page,
                            self.list_max_show_all, self.list_editable, self, None
                            )

            return cl.get_queryset(request)

Update: Alternative solution #2: I’m also capable of exporting using ImportExportActionModelAdmin, so I’m just using that class.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

9reactions
msrshahrukh100commented, Aug 18, 2018

when will the new release be coming? i am still facing this issue

4reactions
bmihelaccommented, Aug 7, 2018

@fmirdita thanks for opening this issue

new keyword argument sortable_by was added in Django 2.1: https://docs.djangoproject.com/en/2.1/ref/contrib/admin/#django.contrib.admin.ModelAdmin.get_sortable_by

Read more comments on GitHub >

github_iconTop Results From Across the Web

django-import-export can not get it working - Stack Overflow
I have used this module successfully in the past and my admin classes inherited only from ImportExportModelAdmin. Maybe the multiple inheritance ...
Read more >
Unable to export using ImportExportModelAdmin - Bountysource
Using 'import-export v==1.0.1'. This is my stack trace: Traceback: File "/Users/fmirdita/Projects/django/django/core/handlers/exception.py" ...
Read more >
Import/Export Files in Django via the Admin Panel - Medium
Hit the Corfirm Import button to import the data. Export: When you click on the export button, you will be able to export...
Read more >
Getting started - Django import / export - Read the Docs
To integrate django-import-export with our Book model, we will create a ModelResource class in admin.py that will describe how this resource can be...
Read more >
Django Import Export data witjout ID
I tried to export data from Django to Excel, but every thing walk behind ... ImportExportModelAdmin from .models import * from import_export ...
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