Skip to content
Advertisement

How to pass a filter from a dropdown into django-import-export view

I understand on how to pass a filter through views that have the return render(request, 'htmlname.html, {}). I don’t know how to do it for this case of exporting data through django-import-export export option. I’d like to pass a filter from a dropdown selection for the data to be downloaded. My view

JavaScript

My resource

JavaScript

Advertisement

Answer

There are a couple of approaches you could take. The approach you choose might depend on whether this is a “one-off” or if you need a reusable class. I would probably favour ‘method 2’.

Method 1

You could filter the queryset in your view, and then pass this to export:

JavaScript

Method 2

Override the get_queryset() method on your resource. You can set args at instantiation and have them apply to the query:

JavaScript

If you look at the source for export() it should make it clearer what is going on, and how this fits together.

Advertisement