Django

Code

Changeset 9202

Show
Ignore:
Timestamp:
10/08/08 03:38:33 (3 months ago)
Author:
mtredinnick
Message:

Fixed #6748 -- When printing the repr() of querysets, don't load 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".

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/db/models/query.py

    r8807 r9202  
    1515CHUNK_SIZE = 100 
    1616ITER_CHUNK_SIZE = CHUNK_SIZE 
     17 
     18# The maximum number of items to display in a QuerySet.__repr__ 
     19REPR_OUTPUT_SIZE = 20 
    1720 
    1821# Pull into this namespace for backwards compatibility. 
     
    142145 
    143146    def __repr__(self): 
    144         return repr(list(self)) 
     147        data = list(self[:REPR_OUTPUT_SIZE + 1]) 
     148        if len(data) > REPR_OUTPUT_SIZE: 
     149            data[-1] = "...(remaining elements truncated)..." 
     150        return repr(data) 
    145151 
    146152    def __len__(self): 
  • django/trunk/tests/regressiontests/queries/models.py

    r9201 r9202  
    557557# ordering, but that isn't needed here. 
    558558>>> qs = Item.objects.order_by('name') 
    559 >>> qs 
     559>>> list(qs) 
    560560[<Item: four>, <Item: one>, <Item: three>, <Item: two>] 
    561561>>> len(qs.query.tables)