Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[1.0.X] Fixed #6748 -- When printing the repr() of querysets, don't l…

…oad or

display more than 20 objects.

This means that accidentally executing HugeStoryArchive.objects.all() at the
interactive prompt (or in the debug template) won't try to load all 4,233,010
stories into memory and print them out. That would previously cause resource
starvation and other "interesting" crashes.

If you really, really want the previous behaviour (e.g. in a doctest that
prints more than 20 items), display "list(qs)" instead of just "qs".

Backport of r9202 from trunk.


git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.0.X@9205 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 4b687d01ba24f0816aa10b9c89c35ee909bb9ee5 1 parent 09d84c4
@malcolmt malcolmt authored
View
8 django/db/models/query.py
@@ -15,6 +15,9 @@
CHUNK_SIZE = 100
ITER_CHUNK_SIZE = CHUNK_SIZE
+# The maximum number of items to display in a QuerySet.__repr__
+REPR_OUTPUT_SIZE = 20
+
# Pull into this namespace for backwards compatibility.
EmptyResultSet = sql.EmptyResultSet
@@ -141,7 +144,10 @@ def __getstate__(self):
return obj_dict
def __repr__(self):
- return repr(list(self))
+ data = list(self[:REPR_OUTPUT_SIZE + 1])
+ if len(data) > REPR_OUTPUT_SIZE:
+ data[-1] = "...(remaining elements truncated)..."
+ return repr(data)
def __len__(self):
# Since __len__ is called quite frequently (for example, as part of
View
2  tests/regressiontests/queries/models.py
@@ -556,7 +556,7 @@ def __unicode__(self):
# automatically. Item normally requires a join with Note to do the default
# ordering, but that isn't needed here.
>>> qs = Item.objects.order_by('name')
->>> qs
+>>> list(qs)
[<Item: four>, <Item: one>, <Item: three>, <Item: two>]
>>> len(qs.query.tables)
1
Please sign in to comment.
Something went wrong with that request. Please try again.