filter on django-import-export
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)
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)
Pass queryset
to export method:
queryset = Price.objects.exclude(...)
data = ExportData().export(queryset)
data.csv