Skip to content

Commit

Permalink
Merge pull request django-import-export#131 from Kobold/allow_iterables
Browse files Browse the repository at this point in the history
Allow Resource.export to accept iterables as well as querysets.
  • Loading branch information
bmihelac committed Aug 21, 2014
2 parents a371862 + 79c94c5 commit e4ab2ae
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
12 changes: 9 additions & 3 deletions import_export/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from django.utils import six
from django.db import transaction
from django.db.models.fields import FieldDoesNotExist
from django.db.models.query import QuerySet
from django.db.models.related import RelatedObject
from django.conf import settings

Expand Down Expand Up @@ -390,9 +391,14 @@ def export(self, queryset=None):
queryset = self.get_queryset()
headers = self.get_export_headers()
data = tablib.Dataset(headers=headers)
# Iterate without the queryset cache, to avoid wasting memory when
# exporting large datasets.
for obj in queryset.iterator():

if isinstance(queryset, QuerySet):
# Iterate without the queryset cache, to avoid wasting memory when
# exporting large datasets.
iterable = queryset.iterator()
else:
iterable = queryset
for obj in iterable:
data.append(self.export_resource(obj))
return data

Expand Down
4 changes: 4 additions & 0 deletions tests/core/tests/resources_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ def test_export(self):
dataset = self.resource.export(Book.objects.all())
self.assertEqual(len(dataset), 1)

def test_export_iterable(self):
dataset = self.resource.export(list(Book.objects.all()))
self.assertEqual(len(dataset), 1)

def test_get_diff(self):
book2 = Book(name="Some other book")
diff = self.resource.get_diff(self.book, book2)
Expand Down

0 comments on commit e4ab2ae

Please sign in to comment.