Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override "remaining elements truncated" in Python

Tags:

python

django

I'm using the Python shell in Django to make some queries. The results keep getting truncated. I get the message, "remaining elements truncated." How can I see all the results? Or, how can I write the results to a file?

like image 537
Wally Avatar asked Jul 14 '10 18:07

Wally


3 Answers

Querysets do this automatically when you just output them in the shell - which implictly calls repr on them. If you call list on the queryset instead, that will output everything:

list(MyModel.objects.all())

Note that you don't need to do this within your code, this is just for output within the shell. Obviously, beware of doing this on a model with a very large number of entries.

like image 141
Daniel Roseman Avatar answered Nov 03 '22 07:11

Daniel Roseman


The top answer returns an error for me in 2020:

Error in argument: '(MyModel.objects.all())'

What works for me is just iterating over the Queryset as a list comprehension:

[i for i in MyModel.objects.all()]
like image 3
n_moen Avatar answered Nov 03 '22 07:11

n_moen


Say your query is:

>>> Foo.objects.all()

Instead try:

>>> for x in Foo.objects.all(): print x

Or to dump them to a file:

>>> f = open('your_filename','w')
>>> for x in Foo.objects.all(): f.write(u'%s\n' % x)
>>> f.close() 
like image 2
Matthew Christensen Avatar answered Nov 03 '22 05:11

Matthew Christensen