filter on django-import-export
Asked Answered
C

4

12

I want to create csv file form my model send a dictionary of Queries ( filter exclude ) to django-import-export and return data from my model.

using flowing code i have all of model fields but i need to filter them with a query.

from import_export import resources
from finance.models import Price

class ExportData(resources.ModelResource):

    class Meta:
        model = Price
Confucianism answered 22/7, 2013 at 14:36 Comment(1)
What do you exactly want to filter?Thermoluminescence
D
13

Pass queryset to export method:

queryset = Price.objects.exclude(...)
data = ExportData().export(queryset)
data.csv
Digastric answered 22/7, 2013 at 15:21 Comment(2)
I just noticed by logging the SQL queries django actually makes, this apparently evaluates the queryset and then makes queryset.count() infividual SQL queries to generate the CSV. Not coolElvera
I followed the instructions from this answer and now I get this error: 'QuerySet' object has no attribute 'before_export'. It make no sense to me since the exceptions raises at the export method of import_export.resources.Resource instead of the django.models.query.QuerySet class. What am I missing?Carduaceous
O
6

To filter only Exported file and not the actual list in the Admin screen you can overwrite the get_export_queryset method

from import_export import resources
from import_export.admin import ImportExportMixin

class ProductAdmin(ImportExportMixin, admin.ModelAdmin):
    resource_class = ProductResource

    # Override of ImportExportMixin.get_export_queryset
    # Filter export to exclude Products where is_active is false
    def get_export_queryset(self, request):
            return Product.objects.filter(is_active=True)
Odo answered 16/7, 2019 at 18:27 Comment(3)
Can't find this in the docs django-import-export.readthedocs.io/en/latest/…Trescott
@ecairol.@Omida Raha. How would I do it if incase my filter is to come from a dropdown selection???Crump
"get_export_queryset" worked perfectly in my case, thank you very much.Colorblind
P
4

You can override the export method of resources.ModelResource in your admin.py file, to apply your filter on admin:

from import_export import resources
from finance.models import Price

class ExportData(resources.ModelResource):

    class Meta:
        model = Price

    def export(self, queryset=None, *args, **kwargs):
        # For example only export objects with ids in 1, 2, 3 and 4
        queryset = queryset and queryset.filter(id__in=[1, 2, 3, 4])
        return super(ExportData, self).export(queryset, *args, **kwargs)
Plowboy answered 7/3, 2019 at 14:42 Comment(1)
@Omida Raha. How would I do it if incase my filter is to come from a dropdown selection???Crump
C
0

You can just modify a export method of ModelResource class.

from import_export import resources
from finance.models import Price

class ExportData(resources.ModelResource):

    class Meta:
        model = Price
    
    def export(self, queryset = None, *args, **kwargs):
        queryset = queryset.exclude(id = 5)
        return super().export(queryset, *args, **kwargs)
Cosecant answered 28/1, 2023 at 6:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.