Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calling len() on a QuerySet object mid-iteration stops iteration at current hundred documents #1086

Closed
eli-b opened this issue Jul 26, 2015 · 7 comments · Fixed by #1427
Closed
Assignees

Comments

@eli-b
Copy link
Contributor

eli-b commented Jul 26, 2015

Please see PR #1072 for a test demonstrating this behavior.

@eli-b
Copy link
Contributor Author

eli-b commented Aug 22, 2015

Anybody?

@touilleMan
Copy link
Member

Hi, sorry for the delay of the response...

Concerning your issue, it's obviously a cache trouble: the QuerySet fetches the items lazily.
This way the first iteration builds the cache at the same time and returns when the cache is done.
If the first iteration (building the cache) another call on the object ask for the items, the 2nd items iteration will build the cache and the first iteration next statement will returns just like the iteration was over.

Fixing this issue seems a bit complicated given we should have a lazy cache that could be partially populated (chunk by chunk) by any iterator (each keeping it own current state)

I think it could be easier to add some information about this behavior in the documentation (and to raise an exception if the reentrant call is done on the object) and to redirect the user on the QuerySetNoCache object which don't have this limitation.

@eli-b
Copy link
Contributor Author

eli-b commented Sep 10, 2015

I'd be happy with an exception being raised - that's a lot better than not knowing you're getting incomplete results.

@gordol
Copy link

gordol commented Apr 23, 2016

I made a pass at fixing this, and it seems to be working now, but I'm not sure whether this will have further ramifications... I'm not familiar enough with how these QuerySets are being implemented to know this won't break anything... but... your test passes now, and I'm using this change in production with no noticeable issues yet... knock on wood

@gordol
Copy link

gordol commented Apr 23, 2016

Also, perhaps related? When I set no_cache() I get the following error when trying to check the length of the QuerySet:

TypeError: object of type 'QuerySetNoCache' has no len()

This happening is what prompted me to try to fix this.

@wojcikstefan
Copy link
Member

I just proposed a proper fix for this in #1427

@eli-b
Copy link
Contributor Author

eli-b commented Dec 6, 2016

Thanks for the fix @wojcikstefan!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants