Saturday, November 17, 2012

Reversed Object has no len()

This was a fairly annoying problem:

To show our images in a reversed order (with the newest at the top), in views I wrote:

images = reversed(list.image_set.all())

This created a problem when you're trying to use len(). I was trying to find the length of the queryset to implement a Facebook-like infinite scroll.

We solved it by creating a QuerySet that reversed the order before outputting:

images = list.image_set.all().order_by('-id')

Hope this helps anybody who had the same problem

No comments:

Post a Comment