Unable to export using ImportExportModelAdmin
See original GitHub issueUsing ‘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:
- Created 5 years ago
- Reactions:2
- Comments:8 (3 by maintainers)
Top GitHub Comments
when will the new release be coming? i am still facing this issue
@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