13👍
✅
Pass queryset
to export method:
queryset = Price.objects.exclude(...)
data = ExportData().export(queryset)
data.csv
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)
- Django's annotate Count with division returns integer instead of float
- Programmatically sync the db in Django
- How do I use request.META.get('HTTP_REFERER') within template?
- How do I update an object's members using a dict?
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)
- Django-rest-framework : list parameters in URL
- Systemctl strange error: Invalid arguments
- How does Django foreign key access work
- Do I need to close connection in mongodb?
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)
Source:stackexchange.com